标签:style blog http color 使用 strong
【为什么要用多线程?】
传统的图形用户界面应用程序都仅仅有一个运行线程,而且一次仅仅运行一个操作。假设用户从用户界面中调用一个比較耗时的操作,当该操作正在运行时,用户界面一般会冻结而不再响应。这个问题能够用事件处理和多线程来解决。
【Linux有线程的概念吗?】
传统的UNIX系统也支持线程的概念,但一个进程里仅仅同意有一个线程,这样多线程就是多进程。Linux下的Posix线程(pthreads)是一种轻量级的进程的移植性实现,线程的调度由内核完毕,每一个线程都有自己的编号。假设使用线程,整体消耗的系统资源较少,线程间通信也比較easy,在project中推荐使用线程。
【使用多线程有什么优点?】
【Qt中创建线程的方法】
仅仅须要子类化QThread并又一次实现它的run()函数就能够了。run()是个纯虚函数,是线程运行的入口,在run()里出现的代码将会在另外线程中被运行。run()函数是通过start()函数来实现调用的。
【实例】
以下一个样例给出了在应用程序中除了主线程外,还提供了线程A和B。假设单击窗体中的button“Start A”,Qt的控制台就会连续输出字母“A”,此时button“Start A”被刷新为“Stop A”。再单击button“Start B”,控制台会交替输出字母“A”和“B”。假设再单击button“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 }
注:
标签:style blog http color 使用 strong
原文地址:http://www.cnblogs.com/mengfanrong/p/3855577.html