#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThreadPool>
#include <QtConcurrent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_nMax = 20;
m_pDlgProgress = new C_DlgProgress(this);
m_pDlgProgress->setRange(0, m_nMax);
m_pDlgProgress->close();
// 1. 继承QThread
m_pTread = new C_Thread(m_nMax, this);
connect(m_pTread, &C_Thread::emit_sendValue, this, [=](int nValue){
m_pDlgProgress->setValue(nValue);
});
// 2. 继承QRunnable
m_pThreadRunnable = new C_ThreadRunnable(m_nMax);
connect(m_pThreadRunnable, &C_ThreadRunnable::emit_sendValue, this, [=](int nValue){
m_pDlgProgress->setValue(nValue);
});
// 3. 继承QObject
m_pThreadObject = new C_ThreadObject(m_nMax);
connect(this, &MainWindow::emit_start, m_pThreadObject, &C_ThreadObject::slot_dealValue);
connect(m_pThreadObject, &C_ThreadObject::emit_sendValue, this, [=](int nValue){
m_pDlgProgress->setValue(nValue);
});
m_pThreadUsedObject = new QThread(this);
m_pThreadObject->moveToThread(m_pThreadUsedObject);
m_pThreadUsedObject->start();
// 0. 在主线程处理耗时操作
connect(this, &MainWindow::emit_sendValue, this, [=](int nValue){
m_pDlgProgress->setValue(nValue);
});
}
MainWindow::~MainWindow()
{
if(m_pThreadUsedObject)
{
m_pThreadUsedObject->quit();
m_pThreadUsedObject->wait();
}
delete ui;
}
void MainWindow::dealValue()
{
int i = 0;
while (i < m_nMax) {
QThread::msleep(100);
++i;
emit emit_sendValue(i);
}
}
void MainWindow::on_btn_TimeConsumingOperation_clicked()
{
m_pDlgProgress->show();
// 0. 在主线程模拟处理耗时操作,界面卡死
// int i = 0;
// while (i < m_nMax) {
// QThread::msleep(100);
// ++i;
// emit emit_sendValue(i);
// }
/*********** 使用哪个,打开哪个注释 ***********/
// 1. 继承QThread
m_pTread->start();
// 2. 继承QRunnable
// QThreadPool::globalInstance()->start(m_pThreadRunnable);
// 3. 继承QObject
// emit emit_start();
// 4. 使用QtConcurrent模块
// QtConcurrent::run(this, &MainWindow::dealValue);
}
没有合适的资源?快使用搜索试试~ 我知道了~
C++&&Qt + 进度框&&线程使用 + 处理耗时操作
共13个文件
cpp:6个
h:5个
ui:1个
需积分: 0 5 下载量 190 浏览量
2024-01-02
18:25:59
上传
评论
收藏 5KB RAR 举报
温馨提示
完整示例代码,可直接运行,内有注释 使用C++和Qt实现线程处理耗时操作,主线程加载处理进度显示 使用4种线程处理方式: 1. 继承QThread 2. 继承QRunnable 3. 继承QObject,移动线程中处理 4. 使用QtConcurrent
资源推荐
资源详情
资源评论
收起资源包目录
progressDlgAndThread.rar (13个子文件)
progressDlgAndThread
Thread.cpp 258B
mainwindow.h 871B
ThreadRunnable.h 402B
Thread.h 325B
mainwindow.cpp 2KB
ThreadObject.cpp 343B
main.cpp 183B
ThreadObject.h 364B
mainwindow.ui 1KB
DlgProgress.cpp 1022B
progressDlgAndThread.pro 1KB
ThreadRunnable.cpp 287B
DlgProgress.h 266B
共 13 条
- 1
资源评论
FreeLikeTheWind.
- 粉丝: 1522
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 高性能量化工具 hikyuu 2.0.3 python3.9 ubuntu 安装包
- Cyclone Version 9.51
- 高性能量化回测工具 hikyuu 2.0.3 python 3.12 windows 安装包
- 省级城乡居民基本养老保险情况数据集(2010-2022年).xlsx
- 舞队填写版.cpp
- 基于BP神经网络的多输入单输出回归预测.zip
- 高性能量化回测工具 hikyuu 2.0.3 python 3.9 windows 安装包
- 省级城镇职工基本养老保险情况2000-2022年.xlsx
- 高性能量化回测工具 hikyuu 2.0.3 python 3.10 windows 安装包
- 算法部署-使用OpenVINO+C#部署PaddleOCR字符识别算法-项目源码-优质项目实战.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功