1【QT】多线程
# ifndef MYWIDGET_H
# define MYWIDGET_H # include <QWidget> namespace Ui {
class MyWidget ;
} class MyWidget : public QWidget
{ Q_OBJECTpublic : explicit MyWidget ( QWidget * parent = 0 ) ; ~ MyWidget ( ) ;
signals: void notify ( ) ; private slots: void on_pushButton_clicked ( ) ; private : Ui:: MyWidget * ui; QThread * pThread;
} ; # endif # ifndef WORKER_H
# define WORKER_H # include <QObject>
class Worker : public QObject
{ Q_OBJECT
public : explicit Worker ( QObject * parent = nullptr ) ; signals: public slots: void onNotify ( ) ; private : void doSomeThing ( ) ;
} ; # endif # include "MyWidget.h"
# include <QApplication> int main ( int argc, char * argv[ ] )
{ QApplication a ( argc, argv) ; MyWidget w; w. show ( ) ; return a. exec ( ) ;
} # include "MyWidget.h"
# include "ui_MyWidget.h" # include <QThread>
# include "Worker.h" MyWidget :: MyWidget ( QWidget * parent) : QWidget ( parent) , ui ( new Ui:: MyWidget)
{ ui-> setupUi ( this ) ;
pThread= new QThread ( this ) ; Worker * p= new Worker; p-> moveToThread ( pThread) ; connect ( pThread, & QThread:: finished, p, & Worker:: deleteLater) ; connect ( this , & MyWidget:: notify, p, & Worker:: onNotify) ; pThread-> start ( ) ;
} MyWidget :: ~ MyWidget ( )
{ pThread-> quit ( ) ; pThread-> wait ( ) ; delete ui;
}
void MyWidget :: on_pushButton_clicked ( )
{ emit notify ( ) ;
} # include "Worker.h"
# include <QThread>
# include <QDebug>
Worker :: Worker ( QObject * parent) : QObject ( parent)
{ }
void Worker :: onNotify ( )
{ doSomeThing ( ) ;
}
void Worker :: doSomeThing ( )
{ for ( int i= 0 ; i< 10 ; ++ i) { QThread :: sleep ( 1 ) ; qDebug ( ) << i; }
}
这里特别需要注意一种【ui】调试方法:【qdebug】调试
2【QT】进度条
用于描述程序执行状态
2.1利用行编辑器显示子线程执行状态
# ifndef MYWIDGET_H
# define MYWIDGET_H # include <QWidget> namespace Ui {
class MyWidget ;
} class MyWidget : public QWidget
{ Q_OBJECTpublic : explicit MyWidget ( QWidget * parent = 0 ) ; ~ MyWidget ( ) ;
signals: void notify ( ) ; private slots: void on_pushButton_clicked ( ) ; void onFn ( int n) ; private : Ui:: MyWidget * ui; QThread * pThread;
} ; # endif # ifndef WORKER_H
# define WORKER_H # include <QObject>
class Worker : public QObject
{ Q_OBJECT
public : explicit Worker ( QObject * parent = nullptr ) ; signals: void fn ( int n) ; public slots: void onNotify ( ) ; private : void doSomeThing ( ) ;
} ; # endif # include "MyWidget.h"
# include <QApplication> int main ( int argc, char * argv[ ] )
{ QApplication a ( argc, argv) ; MyWidget w; w. show ( ) ; return a. exec ( ) ;
} # include "MyWidget.h"
# include "ui_MyWidget.h" # include <QThread>
# include "Worker.h" MyWidget :: MyWidget ( QWidget * parent) : QWidget ( parent) , ui ( new Ui:: MyWidget)
{ ui-> setupUi ( this ) ;
pThread= new QThread ( this ) ; Worker * p= new Worker; p-> moveToThread ( pThread) ; connect ( pThread, & QThread:: finished, p, & Worker:: deleteLater) ; connect ( this , & MyWidget:: notify, p, & Worker:: onNotify) ; connect ( p, & Worker:: fn, this , & MyWidget:: onFn) ; pThread-> start ( ) ;
} MyWidget :: ~ MyWidget ( )
{ pThread-> quit ( ) ; pThread-> wait ( ) ; delete ui;
}
void MyWidget :: on_pushButton_clicked ( )
{ emit notify ( ) ;
}
void MyWidget :: onFn ( int n)
{ ui-> lineEdit-> setText ( QString :: asprintf ( "%d" , n) ) ;
} # include "Worker.h"
# include <QThread>
# include <QDebug>
Worker :: Worker ( QObject * parent) : QObject ( parent)
{ }
void Worker :: onNotify ( )
{ doSomeThing ( ) ;
}
void Worker :: doSomeThing ( )
{ for ( int i= 0 ; i< 10 ; ++ i) { QThread :: sleep ( 1 ) ;
# if 0 qDebug ( ) << i;
# endif # if 1 emit fn ( i) ;
# endif }
}
2.2利用进度条显示子线程执行状态
实现方法:在进度条槽函数中添加set方法,使得进度条当前值与槽函数接收到的信号值同步,就实现了进度条动态增长。
void MyWidget :: onFn ( int n)
{ ui-> lineEdit-> setText ( QString :: asprintf ( "%d" , n) ) ; ui-> progressBar-> setValue ( n) ;
}