在它的“超时”信号/槽函数中停止 Qt 的计时器是否安全?似乎无法在 Qt 文档中找到有关QTimer的任何信息。
我创建了一个计时器,它定期向服务器发送“保持活动”消息。如果在发送我的消息时出现某种错误,我希望停止此计时器。
private:
QTimer* mpKeepAliveTimer;
定时器初始化如下:
mpKeepAliveTimer = new QTimer(/* this */);
QObject::connect(mpKeepAliveTimer, SIGNAL(timeout()), this, SLOT(OnKeepAlive()));
mpKeepAliveTimer->start(KEEP_ALIVE_PERIOD);
像这样停止:
if (mpKeepAliveTimer != NULL) // <-- Edited
{
if (mpKeepAliveTimer->isActive() == true)
mpKeepAliveTimer->stop();
delete mpKeepAliveTimer;
mpKeepAliveTimer = NULL;
}
超时函数如下所示:
void Classname::OnKeepAlive()
{
if (isErrorFound == true)
mpKeepAliveTimer->stop(); // <---- IS THIS SAFE?
}
谢谢。