Qt: qtimer and qthread

Source: Internet
Author: User

Let qtimer run in other threads. The general statement is as follows.

1. Specify a timer for the worker thread in main thread.

QThread* thread = new QThread(this);thread->start();QTimer *timer = new QTimer(0);timer->setInterval(100);timer->moveToThread(thread);connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()), Qt::DirectConnection);connect(thread, SIGNAL(started()), timer,SLOT(start()));

  

Note the following.

1) qtimer cannot specify the parent; otherwise, a warning is displayed, "qobject: movetothread: cannot move objects with a parent"

Because movetothread cannot move objects with parent.

2) qtimer needs to use movetothread to change thread relevance so that emit signal will be in the worker thread.

3) During connect timeout, the QT: directconnection parameter must be added,

According to the documents in QT

Qt::AutoConnection0(default) If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection is determined when the signal is emitted.Qt::DirectConnection1The slot is invoked immediately, when the signal is emitted.Qt::QueuedConnection2The slot is invoked when control returns to the event loop of the receiver‘s thread. The slot is executed in the receiver‘s thread.

The default connect parameter is autoconnection, so when the slot object is the main thread, it will automatically post the event to the main thread. If directconnection is specified, the slot will be called directly. That is, it will be processed in the worker thread.

4) if you directly use timer-> Start ();, a warning will be displayed: qobject: starttimer: timers can only be used with threads started with qthread.

Timer can only be created and started in the same thread. (You can modify it using movetothread.) It seems that the description of "same thread" is not accurate, but this is probably the meaning.

connect(thread, SIGNAL(started()), timer,SLOT(start()));

So you need to start it like this.

In fact, it can also be tricky:

QThread* thread = new QThread(this);thread->start();QTimer *timer = new QTimer(0);timer->setInterval(100);connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()), Qt::DirectConnection);//connect(thread, SIGNAL(started()), timer,SLOT(start()));timer->start(); timer->moveToThread(thread);

5) timer does not specify a parent, so it will not be destroyed automatically.

 

2. Start qtimer in the worker thread.

class Worker :public QThread {Q_OBJECTpublic:Worker(MyClass *parent); virtual ~Worker(){}void run();MyClass *timerReceiver; };Worker::Worker(MyClass *parent): QThread(parent) {timerReceiver = parent;}void Worker::run(){QTimer *timer = new QTimer(this);timer->setInterval(100);connect(timer, SIGNAL(timeout()), timerReceiver, SLOT(onTimeout()), Qt::DirectConnection);timer->start();exec();return;}

Myclass is a UI window, similar to the following.

class MyClass : public QMainWindow{Q_OBJECTpublic:MyClass(QWidget *parent = 0);~MyClass();public slots:void onTimeout(); private:Ui::MyClassClass ui;};MyClass::MyClass(QWidget *parent): QMainWindow(parent){ui.setupUi(this);Worker *thread = new Worker(this);//QThread* thread = new QThread(this);thread->start();}MyClass::~MyClass(){}void MyClass::onTimeout(){}

So. We can see that if timer is created in the worker thread, it does not need movetothread to modify thread relevance.

Timer-> Start () can also be called directly.

However, you still need to specify QT: directconnection. Because timerreceiver is in main thread.

If timerreceiver is also created in the worker thread, you do not need to specify QT: directconnection.

void Worker::run(){TestObject *timerReceiver = new TestObject(this);QTimer *timer = new QTimer(this);timer->setInterval(100);connect(timer, SIGNAL(timeout()), timerReceiver, SLOT(onTimeout()));timer->start();exec();return;}

  

class TestObject : public QObject {Q_OBJECTpublic:TestObject(QObject *parent) : QObject(parent) {}public slots :void onTimeout(){}};

  

It looks exactly the same as what we use in peacetime. → _ →

 

Qt: qtimer and qthread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.