Starting QTimer In A QThread

前端 未结 6 2251
醉梦人生
醉梦人生 2020-12-01 11:05

I am trying to start a QTimer in a specific thread. However, the timer does not seem to execute and nothing is printing out. Is it something to do with the timer, the slot o

6条回答
  •  臣服心动
    2020-12-01 11:37

    m_thread = new QThread(this);
    QTimer* timer = new QTimer(0); // _not_ this!
    timer->setInterval(1);
    timer->moveToThread(m_thread);
    // Use a direct connection to whoever does the work in order
    // to make sure that doIt() is called from m_thread.
    worker->connect(timer, SIGNAL(timeout()), SLOT(doIt()), Qt::DirectConnection);
    // Make sure the timer gets started from m_thread.
    timer->connect(m_thread, SIGNAL(started()), SLOT(start()));
    m_thread->start();
    

提交回复
热议问题