日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中另一种创建线程的方式

發布時間:2025/4/5 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中另一种创建线程的方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 Qt中另一種創建線程的方式
      • 1.1 另一種創建線程的方式
      • 1.2 同步型線程的設計
      • 1.3 異步型線程的設計

1 Qt中另一種創建線程的方式

1.1 另一種創建線程的方式

歷史的痕跡:

現代軟件架構技術中的參考準則:

  • 盡量使用組合的方式實現系統功能。
  • 代碼中僅體現需求中的集成關系。

思考:

  • 通過集成的方式實現新的線程類有什么實際意義?

事實上:

結論:

  • 通過繼承的方式實現多線程沒有任何實際意義。
  • QThread對應于操作系統中的線程。
  • QThread用于充當一個線程操作的集合。
  • 應該提供靈活的方式指定線程入口函數。
  • 盡量避免重寫void run()。

在新版本的QThread類的實現中:

問題:

  • 如何靈活的指定一個線程對象的線程入口函數?

解決方案-信號與槽:

  • 在類中定義一個槽函數void tmain()作為線程入口函數。
  • 在類中定義一個QThread成員對象m_thread。
  • 改變當前對象的線程依附性到m_thread。
  • 連接m_thread的start()信號到tmain()(一定不要忘了在tmain中調用quit退出線程)。
  • 1.2 同步型線程的設計

    AnotherThread.h:

    #ifndef ANOTHERTHREAD_H #define ANOTHERTHREAD_H#include <QObject> #include <QThread>class AnotherThread : public QObject {Q_OBJECTQThread m_thread; protected slots:void tmain(); public:explicit AnotherThread(QObject *parent = 0);void start();void terminate();void exit(int c);~AnotherThread();};#endif // ANOTHERTHREAD_H

    AnotherThread.cpp:

    #include "AnotherThread.h" #include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent) {moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain())); }void AnotherThread::tmain() {qDebug() << "void AnotherThread::tmain() tid = " << QThread::currentThreadId();for(int i=0; i<10; i++){qDebug() << "void AnotherThread::tmain() i = " << i;}qDebug() << "void AnotherThread::tmain() end";m_thread.quit(); }void AnotherThread::start() {m_thread.start(); }void AnotherThread::terminate() {m_thread.terminate(); }void AnotherThread::exit(int c) {m_thread.exit(c); }AnotherThread::~AnotherThread() {m_thread.wait(); }

    main.cpp:

    #include <QtCore/QCoreApplication> #include <QDebug> #include <QThread> #include "AnotherThread.h"void test() {AnotherThread at;at.start(); }int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);qDebug() << "main() tid = " << QThread::currentThreadId();test();return a.exec(); }

    1.3 異步型線程的設計

    核心要點是在線程結束后調用deletLater函數。

    AnotherThread.h:

    #include "AsyncThread.h" #include <QDebug>AsyncThread::AsyncThread(QObject *parent) :QThread(parent) { }void AsyncThread::run() {for (int i=0; i<5; i++){qDebug() << "MyThread run() : " << i << ", thread id = " << currentThreadId();sleep(1);}deleteLater(); }bool AsyncThread::event(QEvent *e) {qDebug() << "void AsyncThread::event(QEvent *)";return QThread::event(e); }int AsyncThread::exec() {qDebug() << "int AsyncThread::exec()";return QThread::exec(); }AsyncThread::~AsyncThread() {qDebug() << "AsyncThread::~AsyncThread() : " << currentThreadId(); }AsyncThread *AsyncThread::NewInstance() {return new AsyncThread; }

    AnotherThread.cpp:

    #include "AnotherThread.h" #include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent) {m_pThread = QThread::currentThread();moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain())); }AnotherThread::~AnotherThread() {if (m_thread.isRunning()){m_thread.wait();}qDebug() << "AnotherThread::~AnotherThread() : " << QThread::currentThreadId(); }AnotherThread *AnotherThread::NewInstance() {return new AnotherThread; }void AnotherThread::tmain() {for (int i=0; i<5; i++){qDebug() << "void AnotherThread::tmain() : " << i << ", thread id = " << QThread::currentThreadId();}m_thread.quit();moveToThread(m_pThread);deleteLater(); }void AnotherThread::onFinished() {qDebug() << "void AnotherThread::finished()";//delete this; }void AnotherThread::start() {m_thread.start(); }void AnotherThread::exit(int c) {m_thread.exit(c); }void AnotherThread::quit() {m_thread.quit(); }bool AnotherThread::wait(unsigned long t) {return m_thread.wait(t); }void AnotherThread::terminate() {m_thread.terminate(); }

    參考資料:

  • QT實驗分析教程
  • 總結

    以上是生活随笔為你收集整理的Qt中另一种创建线程的方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 亚洲粉嫩 | 日本波多野结衣在线 | 国产一级二级三级视频 | 国产一级视频在线 | 手机午夜视频 | 欧美综合视频 | 69福利网 | 欧美成年人在线视频 | 久久亚洲综合网 | 少妇的被肉日常np | 日韩在线观看免费av | 国产日日日 | 日日骚网 | 亚洲男人天堂2017 | 国产免费无码一区二区视频 | 五十路在线观看 | 性久久久 | 天天干天天舔天天射 | 91在线视频 | 日韩成人中文字幕 | 隣の若妻さん波多野结衣 | 久久久久亚洲av无码麻豆 | 日韩在线观看网址 | 国产日韩在线视频 | 国产51精品 | 国产一区二区三区在线视频 | 黄色a毛片 | www.色综合.com | 国产亚洲色婷婷久久99精品 | 亚洲综合欧美综合 | 中文字幕在线播放 | 国产黄色大片视频 | 999精品国产| 国产午夜一级 | 超鹏在线视频 | 亚洲色偷偷综合亚洲av伊人 | 日日操日日射 | 日韩在线一级 | 99r热| 亚洲理论在线观看 | 最新国产露脸在线观看 | 欧美亚洲大片 | 操大爷影院 | 国产一区二区三区中文字幕 | 国产77777 | 午夜资源 | 国产不卡一二三 | 操bbbbb | 99小视频| aa级黄色片 | 中文字幕超碰在线 | 日本色图片 | 成人免费视频国产在线观看 | 在线观看免费人成视频 | 精品国产一区二区视频 | 在线观看日韩中文字幕 | 午夜片在线观看 | 播播成人网| 最近中文字幕一区二区 | 男女网站在线观看 | 日日干夜夜操 | 哪里可以免费看毛片 | 欧美性生交片4 | 久久久久久久久久久久久久免费看 | 深爱五月激情五月 | 久久黄色片视频 | 日韩欧美高清视频 | 猛男被粗大男男1069 | 琪琪色综合 | 在线观看一二区 | 日韩av一区在线观看 | 亚洲品质自拍视频网站 | 95久久 | 成年人网站在线 | 亚洲欧美在线观看 | 911国产视频| 亚洲四虎av| 精品乱人伦一区二区三区 | 河北彩花av在线播放 | 四虎一区二区 | 超碰97自拍 | 看污网站 | 久草网在线视频 | 男女日批视频 | 天天干天天干天天干天天 | 国产第99页 | 影音先锋国产精品 | 免费看一级黄色大片 | 国产成人av一区二区 | 免费一级片在线观看 | 91欧美国产 | 久久久青草 | 国产资源在线免费观看 | 丁香花电影免费播放在线观看 | 国产成人福利 | 久久夜色精品国产欧美乱 | 国产色网址 | 无码少妇一级AV片在线观看 | 国产黄在线观看 |