<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 概述 1. 利用繼承QObject方法創建多線程,主要的步驟有一下幾點:(注意:退出線程循環后,還要調用QThread::quit()函數,該線程才會觸發`QThread::finished()`信號) **流程** 1. 首先創建一個類MyThread,基類為QObject。 2. 在類MyThread中創建一個槽函數,用于運行多線程里面的代碼。所有耗時代碼,全部在這個槽函數里面運行。 3. 實例一個QThread線程對象(容器),將類MyThread的實例對象轉到該容器中,用函數void QObject::moveToThread(QThread *thread); ``` myObjectThread->moveToThread(firstThread); ``` 4. 用一個信號觸發該多線程槽函數,比如用QThread::started()信號。 ``` connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot())); ``` 5. 用信號QThread::finished綁定槽函數QThread::deleteLatater(),在線程退出時,銷毀該線程和相關資源。 ``` connect(firstThread,SIGNAL(finished()),firstThread,SLOT(deleteLater())); ``` 6. 所有線程初始化完成后,啟動函數QThread::start()開啟多線程,然后自動觸發多線程啟動信號QThread::started()。 ## 示例 <details> <summary> mythread.h</summary> ``` #ifndef MYTHREAD_H #define MYTHREAD_H #include<QThread> #include<QDebug> class MyThread : public QObject { public: explicit MyThread(QObject *parent = nullptr); void closeThread(); public slots: void startThreadSLot(); private: volatile bool isStop; }; #endif ``` </details> <br /> <details> <summary> widget.h</summary> ``` #ifndef WIDGET_H #define WIDGET_H #include "mythread.h" #include <QVBoxLayout> #include <QWidget> class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); void createView(); public slots: void openThreads(); void closeThreads(); void finishedThread(); signals: private: MyThread* myThread; QThread* thread; QVBoxLayout *layout; }; #endif // WIDGET_H ``` </details> <br /> <details> <summary> mythread.cpp</summary> ``` #include "mythread.h" MyThread::MyThread(QObject *parent) : QObject(parent){ isStop=false; } void MyThread::closeThread(){ isStop=true; } void MyThread::startThreadSLot() { while(true){ if(isStop){ return ; } qDebug()<<"MyThread::startThreadSlot QThread::currentThreadId()=="<<QThread::currentThreadId(); QThread::sleep(1); } } ``` </details> <br /> <details> <summary> widget.cpp</summary> ``` #include "widget.h" #include <QPushButton> #include <QTextEdit> Widget::Widget(QWidget *parent) : QWidget(parent) { createView(); thread = new QThread; } void Widget::createView() { qDebug()<<"啟動進程"; layout=new QVBoxLayout(this); QPushButton *openThreadBtn = new QPushButton(tr("打開線程")); QPushButton *closeThreadBtn = new QPushButton(tr("關閉線程")); layout->addWidget(openThreadBtn); layout->addWidget(closeThreadBtn); layout->addWidget(new QTextEdit()); layout->addStretch(); connect(openThreadBtn,&QPushButton::clicked,this,&Widget::openThreads); connect(closeThreadBtn,&QPushButton::clicked,this,&Widget::closeThreads); } void Widget::openThreads() { qDebug()<<tr("開啟線程"); if(myThread==Q_NULLPTR) myThread = new MyThread; if(!thread->isRunning()){ myThread->moveToThread(thread); connect(thread,&QThread::finished,myThread,&MyThread::deleteLater); connect(thread,&QThread::started,myThread,&MyThread::startThreadSLot); thread->start(); qDebug()<<"QThread::currentThreadId() "<<QThread::currentThreadId(); }else{ qDebug()<<"已經開啟"; } } void Widget::closeThreads() { qDebug()<<"關閉線程"; if(thread->isRunning()){ myThread->closeThread(); myThread=nullptr; thread->quit(); thread->wait(); } } void Widget::finishedThread() { qDebug()<<"多線程觸發了"; } ``` </details> <br /> <details> <summary> main.cpp</summary> ``` int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget widget; widget.resize(500,500); widget.show(); return a.exec(); } ``` </details> <br /> </details> <br />
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看