How to use the QT middle thread

Source: Internet
Author: User
Tags mutex

Using threads in QT can improve productivity.

To use a thread, go through four steps:

(1) First create a C + + class file, remember to inherit the thread, create the following steps:

A, the first step

B, Step two

(2) Customizing a run function, the program jumps to the run function when the thread is started later

void run ();

(3) Initialize thread

New Hdthread ();

(4) Start thread

Mythread->start ();

Here's a look at the specific examples of threading usage:

Thread header file Hdthread.h:

1 #ifndef Hdthread_h2 #defineHdthread_h3#include <QThread>4#include <QLabel>5#include <QMutex>6 7 classHdthread: PublicQthread8 {9  Public:TenHdthread (qmutex* mtex,qobject *parent =0); One     voidRun ();//the custom run function A     voidSetLabel (Qlabel *lb); - Private: -Qlabel *label; theQmutex *mutex;//Mutual exclusion Lock - }; -  - #endif //Hdthread_h

Header file for main function threadqt.h

1 #ifndef Threadqt_h2 #defineThreadqt_h3 4#include <QMainWindow>5#include 6#include <writefile.h>7#include <QMutex>8#include <QSemaphore>9 Ten namespaceUi { One classTHREADQT; A } -  - classTHREADQT: PublicQmainwindow the { - Q_object -  -  Public: +     ExplicitTHREADQT (Qwidget *parent =0); -~THREADQT (); +  A      //defining a static signal class at     StaticQsemaphore *sp_a; -     StaticQsemaphore *sp_b; - PrivateSlots: -     voidon_pushbutton_clicked (); -  - Private: inUI::THREADQT *UI; -  toHdthread *thread;//Hdtread class, which inherits the thread +WriteFile *Writethread; - Qmutex mutex;//defining mutex Classes the  * }; $ Panax Notoginseng #endif //Threadqt_h

Source file Hdthread.cpp:

#include"hdthread.h"#include<QDebug>#include<threadqt.h>Hdthread::hdthread (Qmutex*mtex, Qobject *parent): Qthread (parent)//constructor, which is used to initialize the{     This->mutex =Mtex;}voidHdthread::setlabel (Qlabel *lb) {     This->label =lb;}voidHdthread::run ()//functions to execute when starting a thread{     while(true) {Qint64 data= Qrand ()% +;//take a random number//This->mutex->lock ();//lockedThreadqt::sp_a->acquire ();//Request Signal         This->label->SetText (Qstring::number (data)); Sleep (1); Threadqt::sp_b->release ();//Release Signal//This->mutex->unlock ();//UnlockQdebug ()<<"Hello Qt"<<data; }}

Source file Threadqt.cpp

#include"threadqt.h"#include"ui_threadqt.h"//Initializing static variablesQsemaphore *threadqt::sp_a =NULL; Qsemaphore*threadqt::sp_b =NULL; THREADQT::THREADQT (Qwidget*parent): Qmainwindow (parent), UI (NewUI::THREADQT) {UI-&GT;SETUPUI ( This); //creating a Signal objectSp_a =NewQsemaphore (1); Sp_b=NewQsemaphore (0);} THREADQT::~threadqt () {DeleteUI;}voidthreadqt::on_pushbutton_clicked () {thread=NewHdthread (&mutex); Initializing Threads thread->setlabel (ui->label); Thread->start ();//Open ThreadWritethread=NewWriteFile (&mutex); Writethread->setlabel (ui->label); Writethread-start ();}

As you can see, the thread here also uses the mutex (semaphore) to ensure that the thread reads and writes the data without errors, so here's a look at the implementation code.

This->mutex->lock ();//lockout threadqt::sp_a->acquire ();//Request Signal This->label->settext (QString::number ( data)); Sleep (1); Threadqt::sp_b->release ();//Release Signal this->mutex->unlock ();//unlock

How to use the QT middle thread

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.