QThread and QTimer

后端 未结 4 2096
感情败类
感情败类 2020-12-19 19:27

I\'m working on an application developed with Qt 4.6.

I want to create a custom timer that counts in a separate thread. However, I want this timer to be able to send

相关标签:
4条回答
  • 2020-12-19 20:11

    Probable reason can be, your timer object is not in a thread with event loop. Event loop is required to trigger the signals.

    However, I would suggest that you should not go with this approach. Timers use different mechanism on different platform and your code might not behave as expected on different platform.

    0 讨论(0)
  • 2020-12-19 20:12

    You don't need to subclass QThread in this particular case. And in general, abstain from subclassing QThread unless you are sure it is what you need.

    Here is a quick example how to setup a worker and timer in a thread and launch it:

    the worker class:

    class Worker : public QObject
    {
        Q_OBJECT
    public:
        explicit Worker(QObject *parent = 0) : QObject(parent) {}
    
    signals:
        void doSomething();
    
    public slots:
        void trigger() {
            emit doSomething();
        }
    };
    

    main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MainThreadObject o;
    
        QThread *thread = new QThread;
        Worker w;
        QTimer timer;
        timer.setInterval(1000);
    
        timer.moveToThread(thread);
        w.moveToThread(thread);
    
        QObject::connect(thread, SIGNAL(started()), &timer, SLOT(start()));
        QObject::connect(&w, SIGNAL(doSomething()), &o, SLOT(doSomething()));
        QObject::connect(&timer, SIGNAL(timeout()), &w, SLOT(trigger()));
    
        thread->start();
    
        return a.exec();
    }
    

    So, we have the MainThreadObject which represents a QObject derived living in the main thread. We create the timer and Worker object, which is just used to wrap a signal and slot to avoid the need of subclassing QThread. The timer is setup and it and the worker are moved to the new thread, the thread started() signal is connected to the timer start() slot, the worker doSomething() signal is connected to the main thread object doSomething() slot, and finally the timer timeout() signal is connected to the worker trigger() slot. Then the thread is started which initiates the entire chain in the event loop.

    As a result, the MainThreadObject::doSomething() is called every second, with the signal emitted from the secondary thread.

    0 讨论(0)
  • 2020-12-19 20:18

    First, if you subclass from QThread, you have to implement run() method, if not, there is no point of doing that, you can inherit from QObject instead.

    Second, your QTimer has to reside in a thread that runs an event loop. Without an event loop no Qt queued signals can be transmitted. You can launch an event loop by calling exec() in thread's run method:

    void Timer::run() {
        exec();
    }
    
    0 讨论(0)
  • 2020-12-19 20:27

    Try

    QMetaObject::invokeMethod(&timer, "start", Qt::QueuedConnection); //timer->start()
    

    if you want to start timer immediately

    Or

    QMetaObject::invokeMethod(&timer, "start", Qt::QueuedConnection , Q_ARG(int, 1000 )); //timer->start(200)
    

    if you want to start timer after 1000s

    In the Non-GUI thread (QThread or Pthread Callback)

    0 讨论(0)
提交回复
热议问题