Qt thread and Timer

Source: Internet
Author: User

The new thread run must contain exec calls. Otherwise, messages cannot be received.

class myQThr : public QThread{Q_OBJECTpublic:myQThr(QObject *in = NULL):QThread(in){WrTimer = new QTimer(this);connect(WrTimer, SIGNAL(timeout()), this, SLOT(TimerOutWr1()), Qt::DirectConnection);WrTimer->start(2000);}void run(){exec();}private:QTimer *WrTimer;public slots:void TimerOutWr1(){int a = 100;}};#include "qt3.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);qt3 w;w.show();myQThr *myt = new myQThr ;myt->start();return a.exec();}

As long as it is a timer in the constructor of myqthr, the starting event must be in the main function thread. Connect (wrtimer, signal (timeout (), this, slot (timeroutwr1 (), QT: directconnection); uses QT: directconnection or QT: queuedconnection. Because at this time, new threads are all self-built, not to mention running.

Run:

class myQThr : public QThread{Q_OBJECTpublic:myQThr(QObject *in = NULL):QThread(in){}void run(){WrTimer = new QTimer(this);connect(WrTimer, SIGNAL(timeout()), this, SLOT(TimerOutWr1()), Qt::QueuedConnection);WrTimer->start(2000);exec();}private:QTimer *WrTimer;public slots:void TimerOutWr1(){int a = 100;}};

In this way, timeroutwr1 is run in the main thread. Modify as follows:

class myQThr : public QThread{Q_OBJECTpublic:myQThr(QObject *in = NULL):QThread(in){}void run(){WrTimer = new QTimer(this);connect(WrTimer, SIGNAL(timeout()), this, SLOT(TimerOutWr1()), Qt::DirectConnection);WrTimer->start(2000);exec();}private:QTimer *WrTimer;public slots:void TimerOutWr1(){int a = 100;}};

If you use QT: directconnection for connect, it runs in the new thread.

 

 

 

 

Here is a description:

-
Instance code 1:

01 class mthread: Public qthread
02 {03 public: 04 mthread (); 05 ~ Mthread (); 06 virtual void run (); 07 void Foo (); 08... 09 10}; Point 1: The qthread class instance is no different from the ordinary class instance, but the run () function will be different from the instance Code 2: 01 class mdialog: public qdialog 02 {03... 04 mthread * mythread; 05}; 06 mdialog: mdialog () 07 {08 mythread = new mthread; 09... 10} note that in QT, the instance mythread of the qthread object belongs to the thread that created it (thread A, that is, the thread where the mdialog is located, all program code and data of mythread are stored in the same space as mdialog. at this time, mythread is like any common instance of the class defined by myself. however, after mythread-> Start () is called, the code in the run () function of mythread will be executed in the new thread (thread B. all variables declared in the run () function \ instantiated objects belong to thread B. however, all the code of mythread is still stored in thread A, but the "execution" of the run () function is in thread B. in mdialog, use mythread-> Foo ();
Foo () is executed in thread. use connect (this, signal (sigdialogsignal (), mythread, slot (slotthreadslot () in mdialog; When emit sigdialogsignal, it will be executed in thread a where mdialog is located. because mythread and mdialog belong to the same thread, thread can be considered as an instance of a common class. in addition, because the connection method of the connect function is automatic by default, and for the same two objects that belong to a pure breed, the automatic connection will use direct connection, that is, the slot is executed immediately in the thread that sends the signal. instance code 3: 01 # include "mthread. H "02 # include <qdebug> 03 mthread: mthread (qobject * parent) 04: qthread (parent) 05 {06 mytimer. Start (1); 07 connect (& mytimer, signal (timeout (), this, slot (slotprint (); 08} 09 10 mthread ::~ Mthread () 11 {12 13} 14 15 void mthread: Run () 16 {17 for (INT I = 0; I <100; ++ I) {18 for (Int J = 0; j <10000; ++ J) {19 qdebug () <"---------" <I; 20} 21} 22 exec (); 23} 24 25 void mthread: slotprint () 26 {27 qdebug () <"============================== "; 28 29} after running :...
...
--------- 9
========================================================== ======================================
--------- 9
...
...
Cannot be mistaken: In a derived class of the qthread class, the statements in the run () function may be interrupted by the timer timeout slot of the thread during running. (error) In fact, slotprint () is executed in the thread where the mthread instance is created, and the run () function runs in the new thread. Point 2: to receive signal from an object in thread B, the object in thread B must enter exec (). For example, there is an endless loop before exec () and it does not enter exec (), the object in thread B will not receive signal. instance code 4:1 void mthread: Run () 2 {3 while (1) {4 dosomething (); // This loop will never Exit 5} 6 exec (); // if this event cycle cannot enter, the thread will not receive any signal 7} Point 3: the pointer in thread a can point to the object instance created in thread B, this instance belongs to thread B. the pointer is just an address, and the variable/Code of the object instance all belong to thread B. instance code: Class mthread: Public qthread 02 {03 q_object 04 05 public: 06 mthread (qobject * parent = 0); 07 ~ Mthread (); 08 void run (); 09 mprint * mprint; 10}; 11 void mthread: Run () 12 {13 mprint = new mprint; 14 exec (); 15} 16 // The object pointed to by the mprint belongs to another thread, that is, the original thread. instance code: Class mthread: Public qthread 02 {03 q_object 04 05 public: 06 mthread (qobject * parent = 0); 07 ~ Mthread (); 08 void run (); 09 mprint * mprint; 10 private: 11 qtimer * mytimer; 12 13 14 private slots: 15 void slotprint (); 16 void testfoo (); 17}; 18 19 void mthread: Run () 20 {21 mytimer = new qtimer; 22 mprint = new mprint; 23 mytimer-> setinterval (100); 24 connect (mytimer, signal (timeout (), this, slot (testfoo (), QT: directconnection );
25 qtimer: singleshot (0, mytimer, slot (START ()));
26 exec (); 27} and above write run (), mytimer in run () New, that is, mytimer pointer belongs to the old thread, however, the entity of the qtimer instance to which mytimer points is in the new thread, and testfoo () will be executed in the new thread. instance code void mthread: Run () 02 {03 qtimer mytimer; 04 mprint = new mprint; 05 mytimer. setinterval (100); 06 connect (& mytimer, signal (timeout (), this, slot (testfoo (), QT: directconnection); 07 qtimer :: singleshot (0, & mytimer, slot (START (); 08 // testfoo (); 09 exec (); 10} write run (), myti Mer declares in run (), that is, mytimer belongs to a new thread, and testfoo () will also be executed in the new thread. instance code: Class mthread: Public qthread 02 {03 q_object 04 05 public: 06 mthread (qobject * parent = 0); 07 ~ Mthread (); 08 void run (); 09 mprint * mprint; 10 private: 11 qtimer mytimer; 12 13 14 private slots: 15 void slotprint (); 16 void testfoo (); 17}; 18 19 20 void mthread: Run () 21 {22 mprint = new mprint; 23 mytimer. setinterval (100); 24 connect (& mytimer, signal (timeout (), this, slot (testfoo (); 25 qtimer: singleshot (0, & mytimer, slot (START (); 26 // testfoo (); 27 exec (); 28} write run (), testfoo () will Create a mytimer thread for execution. as you can see, mytimer and this (mythread) are both in the same thread, but in another thread (run (), connect is performed. note that you cannot use mytimer to start a timer in thread a in thread B. start (), so the timer cannot be started. instead, use signal to trigger the START () slot. point 5: The slot will not interrupt the slot in the same thread. instance code 8: 01 # include "mthread. H "02 # include <qdebug> 03 mthread: mthread (qobject * parent) 04: qthread (parent) 05 {06 mytimer. start (1); 07 connect (& mytimer, signal (timeout (), this, slot (slotprint (); 08} 09 10 mthread ::~ Mthread () 11 {12 13} 14 15 void mthread: Run () 16 {17 exec (); 18} 19 20 void mthread: slotprint () 21 {22 qdebug () <"=========================== "; 23 For (INT I = 0; I <100; ++ I) {24 for (Int J = 0; j <10000; ++ J) {25 qdebug () <"---------" <I; 26} 27} 28} after the slotprint () function is run, it exits, indicating that the slot will not be interrupted, after a slot is executed, the next slot will be executed. note: slotprint () is executed in the thread that creates the mthread instance. instead of the thread created using thread-> Start. instance code: # Include "mthread. H "02 # include <qdebug> 03 mthread: mthread (qobject * parent) 04: qthread (parent) 05 {06 mytimer. start (1); 07 connect (& mytimer, signal (timeout (), this, slot (slotprint (); 08} 09 10 mthread ::~ Mthread () 11 {12 13} 14 15 void mthread: Run () 16 {17 testfoo (); 18 exec (); 19} 20 21 void mthread: slotprint () 22 {23 qdebug () <"======================= "; 24 25} 26 27 void mthread: testfoo () 28 {29 for (INT I = 0; I <100; ++ I) {30 for (Int J = 0; in the code above j <10000; ++ J) {31 qdebug () <"---------" <I; 32} 33} 34}, slotprint () and testfoo () it will be executed in two different threads.

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.