QTimer的使用

文章描述了一名开发者遇到的bug,即系统在电池电量检查定时器触发下频繁显示关机提示但实际不关机。问题源于定时器间隔设置过短导致alertHideTime被频繁停止。此外,还讨论了QTimer连接函数的问题,指出应在初始化时一次性完成连接,避免重复执行槽函数。
摘要由CSDN通过智能技术生成

        今天报一个bug,自动关机提示界面已经提示,但是一直不关机,打印log看到关机命令已经下发了,但是就是不关机,这是什么原因呢?

        经过同事的帮助,发现关机命令下发了很多次,并且下发的间隔很短,而我原本的设计想法是提示关机,等待两秒后下发关机命令,想必是这里出问题了,不应该下发很多次。查看log发现,电量查询定时器多次调用batteryTimerTimeOut的showAlertWgt,但一直没有调用showAlertWgt里面的延时关机的定时器,直到log的最后却有很多次调用。

[1970-01-01 08:06:13.503] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:15.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:17.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:19.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:21.506] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:23.505] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:25.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:27.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:29.504] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:31.503] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:33.502] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:35.502] [batteryTimerTimeOut]: batteryTimerTimeOut index = 0,poweroff flag = 1,vol = 10,index = 0
[1970-01-01 08:06:37.569] [operator()]: send poweroff finished
[1970-01-01 08:06:37.641] [operator()]: send poweroff finished
[1970-01-01 08:06:37.735] [operator()]: send poweroff finished
[1970-01-01 08:06:37.810] [operator()]: send poweroff finished
[1970-01-01 08:06:37.892] [operator()]: send poweroff finished
[1970-01-01 08:06:37.968] [operator()]: send poweroff finished

代码如下

showAlertWgt(){

         ..........

    alertHideTime->stop();
    QMetaObject::Connection ret = connect(alertHideTime, &QTimer::timeout, myWgt,
        []() {
                system("poweroff");
        }
    );
    alertHideTime->start(2000);

         .........

}

        整个现象就是alertHideTime不被调用,原因是电池check的timer间隔是2s,而alertHideTime的间隔也是2s,在alertHideTime到期之前就被再一次调用showAlertWgt()的时候stop了,所以一直没有下发关机命令(当然,设计之初是没有这个bug的,因为一开始电池刷新的timer间隔是5s,也就不存在alertHideTime被stop的问题了,只是后来驱动同事叫我把间隔缩短,于是有这个问题了,并且那时候没有测出来);而后面为什么又会运行关机命令呢,原因是连接usb以后,系统有供电了,就不会再运行showAlertWgt(),也就不会去stop这个alertHideTime了,因此两秒后时间到了,就会执行关机命令,也就是看到的现象:关机提示语出现,一直不关机,插上usb,马上关机。

        这个log中还有一个问题,关机命令被下发了很多次,原因是QTimer每次stop以后,就去connect了一个函数,也就是QTimer不能被多次connect,应该在init的时候就connect完成,后面就不要再设置了,要不然,之前connect的每个槽都会被执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值