简体   繁体   English

如何在QThread中使用QTimer?

[英]How to use QTimer inside QThread?

I have a class, a subclass of QObject. 我有一个类,它是QObject的子类。 I am creating an instance for that class inside a QThread. 我正在QThread中为该类创建一个实例。 I want to use QTimer in my class. 我想在课堂上使用QTimer。 But the timer is not starting because the caller thread is a woker thread. 但是计时器没有启动,因为调用者线程是一个工作线程。 How can i achieve the solution? 我如何实现解决方案?

Well, can you make a slot in some helper object that lives in main thread and is a member of the class and dispatch the timer creation and start to it from your class (and optionally check if the current thread is not the event loop since then it's safe to start the timer there) ? 好吧,您是否可以在生活在主线程中并且是该类成员的某些辅助对象中创建一个插槽,并调度计时器的创建并从您的类中开始创建它(并且可以选择检查当前线程是否从那时起不是事件循环)在那儿启动计时器很安全)? So then when you need to create the timer you would just fire a signal to your helper object in main thread and it should work. 因此,当您需要创建计时器时,您只需向主线程中的辅助对象发出一个信号,它就可以工作。

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

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