简体   繁体   English

如何完全停止 QTimer

[英]How to completely stop QTimer

How could i completely stop a QTimer ?我怎么能完全停止QTimer when i call QTimer::stop() it doesn't correctly stopped.当我调用QTimer::stop()它没有正确停止。

For example i create a QTimer :例如我创建一个QTimer

QTimer timer;
timer.singleShot(10000, this, [] { emit work_is_down(); });

and when i stopped:当我停下来时:

timer.stop();

the lambda [] { emit work_is_down(); } lambda [] { emit work_is_down(); } [] { emit work_is_down(); } is still run after 10000ms. [] { emit work_is_down(); }仍然在 10000 毫秒后运行。 how to completely stop it?如何完全阻止它?

QTimer::singleShot is a static method of the QTimer class. QTimer::singleShot singleShot 是QTimer class 的static方法。 You can call it on an instance but it will be fundamentally disconnected from that instance.您可以在实例上调用它,但它会从根本上断开与该实例的连接。

You need to do the full QTimer dance, as said in the comments above:您需要完成完整的 QTimer 舞蹈,如上面的评论中所述:

QTimer *timer = new QTimer();
QObject::connect(timer, &QTimer::timeout, this, [](){ emit work_is_down(); });
timer->setSingleShot(true);
timer->start(10000);

Don't forget to call delete timer or timer->deleteLater() when you're done with it, or better yet, use a std::unique_ptr or QSharedPointer .完成后不要忘记调用delete timertimer->deleteLater() ,或者更好的是,使用std::unique_ptrQSharedPointer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM