QT Notes-Multi-threading

Source: Internet
Author: User
Tags emit thread class

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-&GT;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-&GT;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

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.