1, Dialog.h
#define Dialog_h
#include <QDialog>
#include "Mythread.h"
Namespace Ui {
Class Dialog;
}
Class Dialog:public Qdialog
{
Q_object
Public
Explicit Dialog (Qwidget *parent = 0);
~dialog ();
Private Slots:
void on_pushbutton_clicked ();
Private
Ui::D ialog *ui;
Mythread *myp;//here declares a class Mythread that inherits from Pthread, which is equivalent to creating a thread.
};
#endif//Dialog_h
2.dialog.cpp
#include"dialog.h"#include"ui_dialog.h"Dialog::D ialog (Qwidget*parent): Qdialog (parent), UI (NewUi::D ialog) {UI->SETUPUI ( This);} Dialog::~Dialog () {DeleteUI;}voiddialog::on_pushbutton_clicked () {MYP=NewMythread (ui->label); MYP-start ();//automatically executes the run () function in Mythread. Equivalent to calling the Run function//qthread AA; //Aa.start ();}
3, Mythread.h
#ifndef Mythread_h#defineMythread_h#include<QThread>#include<QLabel>classMythread: Publicqthread{ Public: Mythread (Qlabel*l); protected: voidrun ();//Declare a run function, run is a virtual function inheritedPrivate: Qlabel*pp;};#endif //Mythread_h
#include"mythread.h"#include<QDebug>Mythread::mythread (Qlabel*l)//constructor initialization {pp=l;}voidMythread::run () { while(1) {qdebug ()<<"Test"; Sleep (1); PP->settext (" A"); Sleep (1); PP->settext (" -"); }}
Multi-Threading in QT