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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Qt工作笔记-moveToThread的基本使用以及让线程安全退出

發布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt工作笔记-moveToThread的基本使用以及让线程安全退出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序運行截圖如下:

這里是4個線程,對ListWidget進行輸入,

使用MoveToThread,十分簡單,但關閉的時候,會出現這樣的提示:

造成這樣的原因是:

循環還沒有結束,線程就被我們關閉了。

?

解決方法如下:

1.重寫關閉事件;

2.使用本地事件循環,先把循環退出后,再退出線程,即可,

?

程序源碼如下:

insertlistitem.h

#ifndef INSERTLISTITEM_H #define INSERTLISTITEM_H#include <QObject>QT_BEGIN_NAMESPACE class QListWidget; QT_END_NAMESPACEclass InsertListItem : public QObject {Q_OBJECT public:explicit InsertListItem(QListWidget *listWidget,QObject *parent = 0);void stopThread();public slots:void beginToWork();private:QListWidget *m_listWidget;bool m_runStatus; };#endif // INSERTLISTITEM_H

widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QThread>class InsertListItem; class QTimer;namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected:void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;private:Ui::Widget *ui;InsertListItem *m_list[4];QThread m_thread[4]; };#endif // WIDGET_H

insertlistitem.cpp

#include "insertlistitem.h" #include <QThread> #include <QListWidget> #include <QListWidgetItem> #include <QTime> #include <QDebug>InsertListItem::InsertListItem(QListWidget *listWidget, QObject *parent) : QObject(parent) {m_listWidget=listWidget;m_runStatus=true; }void InsertListItem::beginToWork() {for(int i=0;i<1000;i++){if(!m_runStatus){break;}QString msg="ThreadId:"+QString::number((unsigned int)QThread::currentThreadId())+" "+" Time:"+QTime::currentTime().toString("HH:mm:ss");m_listWidget->insertItem(0,msg);QThread::sleep(1);} }void InsertListItem::stopThread() {m_runStatus=false; }

main.cpp

#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }

widget.cpp

#include "widget.h" #include "ui_widget.h" #include "insertlistitem.h" #include <QDebug> #include <QMessageBox> #include <QCloseEvent> #include <QEventLoop> #include <QTimer>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_list[0]=new InsertListItem(ui->listWidget);m_list[0]->moveToThread(&m_thread[0]);m_list[1]=new InsertListItem(ui->listWidget_2);m_list[1]->moveToThread(&m_thread[1]);m_list[2]=new InsertListItem(ui->listWidget_3);m_list[2]->moveToThread(&m_thread[2]);m_list[3]=new InsertListItem(ui->listWidget_4);m_list[3]->moveToThread(&m_thread[3]);connect(&m_thread[0],SIGNAL(started()),m_list[0],SLOT(beginToWork()));m_thread[0].start();connect(&m_thread[1],SIGNAL(started()),m_list[1],SLOT(beginToWork()));m_thread[1].start();connect(&m_thread[2],SIGNAL(started()),m_list[2],SLOT(beginToWork()));m_thread[2].start();connect(&m_thread[3],SIGNAL(started()),m_list[3],SLOT(beginToWork()));m_thread[3].start();}Widget::~Widget() {delete ui; }void Widget::closeEvent(QCloseEvent *event) {QMessageBox::StandardButton button=QMessageBox::information(this,"提示","退出",QMessageBox::Ok,QMessageBox::Cancel);if(button==QMessageBox::Ok){m_list[0]->stopThread();m_list[1]->stopThread();m_list[2]->stopThread();m_list[3]->stopThread();QEventLoop loop;QTimer::singleShot(1*1000,&m_thread[0],SLOT(terminate()));QTimer::singleShot(1*1000,&m_thread[1],SLOT(terminate()));QTimer::singleShot(1*1000,&m_thread[2],SLOT(terminate()));QTimer::singleShot(1*1000,&m_thread[3],SLOT(terminate()));QTimer::singleShot(1.5*1000,&loop,SLOT(quit()));QTimer::singleShot(2*1000,this,SLOT(close()));loop.exec();event->accept();}else{event->ignore();} }

?

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的Qt工作笔记-moveToThread的基本使用以及让线程安全退出的全部內容,希望文章能夠幫你解決所遇到的問題。

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