日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Qt线程初步

發(fā)布時(shí)間:2024/10/12 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt线程初步 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

默默地EEer,原文地址:

http://www.cnblogs.com/hebaichuanyeah/p/5618781.html

在調(diào)用linux環(huán)境下線程API寫Cpp時(shí),發(fā)現(xiàn)異常蛋疼,要么線程函數(shù)不能是類的成員函數(shù),要么必須是類的static成員函數(shù)(意味著調(diào)用的成員變量也必須是static……其實(shí)還是不屬于類)。

Qt提供一套線程機(jī)制,只要繼承QThread基類并重新實(shí)現(xiàn)void run()函數(shù)即可。

新建thread.h繼承Qthread,并在thread.cpp中實(shí)現(xiàn)它。

thread.h

?

#include "QThread"class Thread : public QThread {Q_OBJECT public:Thread();// void setMessage(const Qstring &message);void setRun();void stop();bool getState(); protected:void run(); private:// QString messageStr;volatile bool enable; signals:void OutMessage(QString message); };

?

?thread.cpp,在run函數(shù)中循環(huán)1秒觸發(fā)一個(gè)信號(hào)輸出信息。

?

?

#include "thread.h"Thread::Thread() {enable = true; }void Thread::run() {while(true){if(enable){this->OutMessage(tr("I'm run : thread 1"));msleep(1000);}} } void Thread::stop() {enable = false; } void Thread::setRun() {enable = true; } bool Thread::getState() {return enable; }

?

?mainwindow.h,定義一些Qt控件

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QLabel> #include <QGridLayout> #include <QPushButton> #include <QTextEdit> #include <QDoubleValidator> #include <QComboBox> #include <QTranslator>#include "thread.h"#define PI 3.14592653589class MainWindow : public QMainWindow {Q_OBJECT public:MainWindow();~MainWindow(); private:QWidget* mainWindow;QGridLayout * mainLayout;QTextEdit * messageEdit;QPushButton * clearButton;QPushButton * runStopButton;Thread * backThread; private slots:void updateMessage(QString message);void clearMessage();void runStopThread();};#endif

?mainwindow.cpp

#include "mainwindow.h" #include <QPainter>MainWindow :: MainWindow() {mainWindow = new QWidget;this->setCentralWidget(mainWindow);backThread = new Thread();backThread->start();clearButton = new QPushButton(tr("clear"));clearButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);runStopButton = new QPushButton(tr("stop"));messageEdit = new QTextEdit();messageEdit->setReadOnly(true);mainLayout = new QGridLayout;mainLayout->addWidget(messageEdit,0,0,5,5);mainLayout->addWidget(clearButton,1,5,1,1);mainLayout->addWidget(runStopButton,0,5,1,1);mainWindow->setLayout(mainLayout);QObject::connect(clearButton,SIGNAL(clicked()),this,SLOT(clearMessage()));QObject::connect(runStopButton,SIGNAL(clicked()),this,SLOT(runStopThread()));QObject::connect(backThread,SIGNAL(OutMessage(QString)),this,SLOT(updateMessage(QString)));this->setWindowTitle(tr("thread test~"));this->resize(QSize(300,100));// this->setFixedSize(200,100); }MainWindow :: ~MainWindow() {mainWindow->deleteLater();mainLayout->deleteLater();messageEdit->deleteLater();clearButton->deleteLater();runStopButton->deleteLater(); }void MainWindow::updateMessage(QString message) {messageEdit->append(message); } void MainWindow::clearMessage() {messageEdit->clear(); } void MainWindow::runStopThread() {if(backThread->getState()){backThread->stop();runStopButton->setText(tr("run"));}else{backThread->setRun();runStopButton->setText(tr("stop"));} }

?運(yùn)行:

后臺(tái)線程不斷打印信息,通過(guò)run/stop按鈕停止/啟動(dòng)。

?

同時(shí),Qt提供一套線程同步機(jī)制

QMutex(互斥鎖),QReadWriteLock(讀寫鎖),QSemaphore(信號(hào)量),QWaitCondition(阻塞等待一個(gè)條件)。

使用都很簡(jiǎn)單,其中QWaitCondition,調(diào)用bool wait(QMutex *mutex, unsigned long time = ULONG_MAX)成員函數(shù)阻塞。 void wakeOne()和void wakeAll()喚醒一處和所有阻塞。(類似于C#里面的ManualResetEvent 類,.WaitOne() ? .Set())

?

轉(zhuǎn)載于:https://www.cnblogs.com/hebaichuanyeah/p/5618781.html

總結(jié)

以上是生活随笔為你收集整理的Qt线程初步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。