【为什么要用多线程?】 传统的图形用户界面应用程序都只有一个执行线程,并且一次只执行一个操作。如果用户从用户界面中调用一个比较耗时的操作,当该操作正在执行时,用户界面通常会冻结而不再响应。这个问题可以用事件处理和多线程来解决。 【Linux有线程的概念吗?】 传统的UNIX系统也支持线程的概念,但一个进程里只允许有一个线程,这样多线程就是多进程。Linux下的Posix线程(pthreads)是 一种轻量级的进程的移植性实现,线程的调度由内核完成,每个线程都有自己的编号。如果使用线程,总体消耗的系统资源较少,线程间通信也比较容易,在工程中 推荐使用线程。 【使用多线程有什么好处?】
【Qt中创建线程的方法】 只需要子类化QThread并重新实现它的run()函数就可以了。run()是个纯虚函数,是线程执行的入口,在run()里出现的代码将会在另外线程中被执行。run()函数是通过start()函数来实现调用的。 【实例1】 1,右击项目添加“新文件” 2,选择“C++类”--》“选择”
3,类名“myThread”
4,修改mythread.h文件如下 #ifndef MYTHREAD_H #define MYTHREAD_H #include class myThread : public QThread { Q_OBJECT public:
explicit myThread(QObject *parent = 0); void stop();
signals:
public slots: protected:
//mythread test void run(); private:
//mythread test volatile bool stopped; //使用volatile可以使的它在任何时候保持最新值
}; #endif // MYTHREAD_H 5,修改mythread.cpp文件如下
#include "mythread.h" myThread::myThread(QObject *parent) : QThread(parent) { stopped = false;//初始化为false } //mythread test 停止进程 void myThread::stop() { stopped = true; } //mythread test 运行进程 void myThread::run() { while(!stopped) {
qDebug()<<>"in myThread:%1").arg(i++); }
stopped = false; } 6,在mainwindow.cpp函数中添加 #include "mythread.h"//mythread test myThread mythread;
//fileshare2 文件发送 "确认"按钮 void MainWindow::on_pushButton_fileShare2_send_ok_clicked() { //文件接收路径 //qDebug()<<>"文件接收路径:").trimmed()<<ui->lineEdit_fileShare2_send_path->text().trimmed();
//mythread test mythread.start();
ui->lineEdit_fileShare2_send_path->setText(tr("mythread 运行中。。。").trimmed()); } //fileshare2 文件发送 "取消"按钮 void MainWindow::on_pushButton_fileShare2_send_cancel_clicked() { //清空控件内容 //ui->lineEdit_fileShare2_send_path->clear(); //mythread test if(mythread.isRunning()) {
mythread.stop();
ui->lineEdit_fileShare2_send_path->setText(tr("mythread停止!").trimmed()); }
} 【实例2】 下面一个例子给出了在应用程序中除了主线程外,还提供了线程A和B。如果单击窗口中的按钮“Start A”,Qt的控制台就会连续输出字母“A”,此时按钮“Start A”被刷新为“Stop A”。再单击按钮“Start B”,控制台会交替输出字母“A”和“B”。如果再单击按钮“Stop A”,则控制台只输出字母“B”。 thread.h代码 1 #ifndef THREAD_H 2 #define THREAD_H 3 4 #include <QThread> 5 #include <iostream> 6 7 class Thread : public QThread 8 { 9 Q_OBJECT 10 public: 11 Thread(); 12 void setMessage(QString message); 13 void stop(); 14 15 protected: 16 void run(); 17 void printMessage(); 18 19 private: 20 QString messageStr; 21 volatile bool stopped; 22 }; 23 24 #endif // THREAD_H 注:
thread.cpp代码 1 #include "thread.h" 2 #include <QDebug> 3 4 Thread::Thread() 5 { 6 stopped = false; 7 } 8 9 void Thread::run() 10 { 11 while(!stopped) 12 { 13 printMessage(); 14 } 15 stopped = false; 16 } 17 18 void Thread::stop() 19 { 20 stopped = true; 21 } 22 23 void Thread::setMessage(QString message) 24 { 25 messageStr = message; 26 } 27 28 void Thread::printMessage() 29 { 30 qDebug()<<messageStr; 31 sleep(1); 32 } 注:
threaddialog.h代码 1 #ifndef THREADDIALOG_H 2 #define THREADDIALOG_H 3 4 #include <QPushButton> 5 #include <QDialog> 6 #include <QCloseEvent> 7 #include "thread.h" 8 9 class ThreadDialog : public QDialog 10 { 11 Q_OBJECT 12 13 public: 14 ThreadDialog(QWidget *parent=0); 15 16 protected: 17 void closeEvent(QCloseEvent *event); 18 19 private slots: 20 void startOrStopThreadA(); 21 void startOrStopThreadB(); 22 void close(); 23 24 private: 25 Thread threadA; 26 Thread threadB; 27 QPushButton *threadAButton; 28 QPushButton *threadBButton; 29 QPushButton *quitButton; 30 }; 31 32 #endif // THREADDIALOG_H
threaddialog.cpp代码 1 #include "threaddialog.h" 2 3 ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent) 4 { 5 threadA.setMessage("A"); 6 threadB.setMessage("B"); 7 8 threadAButton = new QPushButton(tr("Start A"), this); 9 threadAButton->setGeometry(10, 30, 80, 30); 10 threadBButton = new QPushButton(tr("Start B"),this); 11 threadBButton->setGeometry(110, 30, 80, 30); 12 quitButton = new QPushButton(tr("Quit"), this); 13 quitButton->setGeometry(210, 30, 80, 30); 14 quitButton->setDefault(true); 15 16 connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA())); 17 connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB())); 18 connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); 19 } 20 21 void ThreadDialog::startOrStopThreadA() 22 { 23 if(threadA.isRunning()) 24 { 25 threadAButton->setText(tr("Stop A")); 26 threadA.stop(); 27 threadAButton->setText(tr("Start A")); 28 } 29 else 30 { 31 threadAButton->setText(tr("Start A")); 32 threadA.start(); 33 threadAButton->setText(tr("Stop A")); 34 } 35 } 36 37 void ThreadDialog::startOrStopThreadB() 38 { 39 if(threadB.isRunning()) 40 { 41 threadBButton->setText(tr("Stop B")); 42 threadB.stop(); 43 threadBButton->setText(tr("Strat B")); 44 } 45 else 46 { 47 threadBButton->setText(tr("Start B")); 48 threadB.start(); 49 threadBButton->setText(tr("Stop B")); 50 } 51 } 52 53 void ThreadDialog::closeEvent(QCloseEvent *event) 54 { 55 threadA.stop(); 56 threadB.stop(); 57 threadA.wait(); 58 threadB.wait(); 59 event->accept(); 60 } 61 62 void ThreadDialog::close() 63 { 64 exit(0); 65 } 注:
main.cpp代码 1 #include "threaddialog.h" 2 #include <QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 ThreadDialog *threaddialog = new ThreadDialog; 8 threaddialog->exec(); 9 return app.exec(); 10 } 注:
|
|