This example is, click the Start button, the number accumulates, click the Stop button, the number does not move.
1, create a new class, inside the content of the child thread
#ifndef Mywidget_h#defineMywidget_h#include<QWidget>#include"mythread.h"#include<QDebug>namespaceUi {classMywidget;}classMywidget: Publicqwidget{Q_object Public: ExplicitMywidget (Qwidget *parent =0); ~Mywidget (); voiddealsignal (); voiddealclose (); Signals:voidStartthread ();//start a child threadPrivateSlots:voidon_startbtn_clicked (); voidon_stopbtn_clicked ();Private: Ui::mywidget*UI; MyThread*MyT; Qthread*thread;};#endif //Mywidget_h
#include"mywidget.h"#include"ui_mywidget.h"#include<QThread>Mywidget::mywidget (Qwidget*parent): Qwidget (parent), UI (Newui::mywidget) {UI->SETUPUI ( This); //dynamically allocating space, cannot specify parent objectMyT =NewMyThread; //Creating child ThreadsThread =NewQthread ( This); //to add a custom thread to a child threadMyt->movetothread (thread); Connect (MyT,&mythread::mysignal, This,&mywidget::d ealsignal); Qdebug ()<<"main thread Number:"<<Qthread::currentthread (); Connect ( This,&mywidget::startthread,myt,&mythread::mytimeout); Connect ( This, &mywidget::d estroyed, This,&mywidget::d ealclose); //inside the threading function, operation of the graphical interface is not allowed//Connect () The function of the fifth parameter, Connection mode: Default, queue, direct//Multithreading makes sense//by Default, if it is multi-threaded, the queue is used by default, and if it is a single thread, the default is to use the direct method//Queue: Slot function The thread is the same as the recipient//Direct: The slot function is the same thread as the sender}voidmywidget::d ealclose () {on_stopbtn_clicked (); DeleteMyT;}voidmywidget::d ealsignal () {Static inti =0; I++; UI->lcdnumber->display (i);} Mywidget::~Mywidget () {DeleteUI;}voidmywidget::on_startbtn_clicked () {if(thread->isrunning () = =true) { return; } //start thread, but no boot threadThread->start (); MyT->setflag (false); //You cannot call a thread handler directly, the direct call causes the thread handler function and the main thread to//myt->mytimeout ();//wrong.//can only be called by Signal-slot modeemit Startthread ();}voidmywidget::on_stopbtn_clicked () {if(thread->isrunning () = =false) { return; } MyT->setflag (true); Thread-quit (); Thread-wait ();}
2, create the Qtread object in the main thread, and then pass the child thread class through Movetothread ()
And does not call the subroutine (sub-thread) directly, sends a signal through the main program, then the subroutine receives the signal, starts to run
#ifndef Mywidget_h#defineMywidget_h#include<QWidget>#include"mythread.h"#include<QDebug>namespaceUi {classMywidget;}classMywidget: Publicqwidget{Q_object Public: ExplicitMywidget (Qwidget *parent =0); ~Mywidget (); voiddealsignal (); voiddealclose (); Signals:voidStartthread ();//start a child threadPrivateSlots:voidon_startbtn_clicked (); voidon_stopbtn_clicked ();Private: Ui::mywidget*UI; MyThread*MyT; Qthread*thread;};#endif //Mywidget_h
#include"mywidget.h"#include"ui_mywidget.h"#include<QThread>Mywidget::mywidget (Qwidget*parent): Qwidget (parent), UI (Newui::mywidget) {UI->SETUPUI ( This); //dynamically allocating space, cannot specify parent objectMyT =NewMyThread; //Creating child ThreadsThread =NewQthread ( This); //to add a custom thread to a child threadMyt->movetothread (thread); Connect (MyT,&mythread::mysignal, This,&mywidget::d ealsignal); Qdebug ()<<"main thread Number:"<<Qthread::currentthread (); Connect ( This,&mywidget::startthread,myt,&mythread::mytimeout); Connect ( This, &mywidget::d estroyed, This,&mywidget::d ealclose); //inside the threading function, operation of the graphical interface is not allowed//Connect () The function of the fifth parameter, Connection mode: Default, queue, direct//Multithreading makes sense//by Default, if it is multi-threaded, the queue is used by default, and if it is a single thread, the default is to use the direct method//Queue: Slot function The thread is the same as the recipient//Direct: The slot function is the same thread as the sender}voidmywidget::d ealclose () {on_stopbtn_clicked (); DeleteMyT;}voidmywidget::d ealsignal () {Static inti =0; I++; UI->lcdnumber->display (i);} Mywidget::~Mywidget () {DeleteUI;}voidmywidget::on_startbtn_clicked () {if(thread->isrunning () = =true) { return; } //start thread, but no boot threadThread->start (); MyT->setflag (false); //You cannot call a thread handler directly, the direct call causes the thread handler function and the main thread to//myt->mytimeout ();//wrong.//can only be called by Signal-slot modeemit Startthread ();}voidmywidget::on_stopbtn_clicked () {if(thread->isrunning () = =false) { return; } MyT->setflag (true); Thread-quit (); Thread-wait ();}
QT Notes-Multi-threading