首页 > 其他分享 >QProgressDialog用法及结合QThread用法,四种线程使用

QProgressDialog用法及结合QThread用法,四种线程使用

时间:2024-01-03 22:32:58浏览次数:35  
标签:nMax 对话框 int void 用法 线程 MainWindow emit QThread

1 QProgressDialog概述

QProgressDialog类提供耗时操作的进度条。 进度对话框用于向用户指示操作将花费多长时间,并演示应用程序没有冻结。此外,QPorgressDialog还可以给用户一个中止操作的机会。 进度对话框的一个常见问题是很难知道何时使用它们;操作在不同的硬件上花费不同的时间。QProgressDialog为这个问题提供了一个解决方案:它估计操作将花费的时间(基于步骤的时间),并且仅在估计超过minimumDuration()(默认为4秒)时才显示它自己。 使用setMinimum()setMaximum()或构造函数设置操作中的“steps”数,并在操作进行时调用setValue()steps数可以任意选择。它可以是复制的文件数、接收的字节数、通过算法主循环的迭代次数,或者其他合适的单位。进度从setMinimum()设置的值开始,当使用setMaximum()设置的值作为参数调用setValue()时,进度对话框显示操作已经完成。 在操作结束时,对话框会自动重置并隐藏自己。使用setAutoReset()setAutoClose()来改变这种行为。 注意,如果设置了一个新的最大值(使用setMaximum()setRange()),它等于你的当前值(),无论如何对话框都不会关闭。

QProgressDialog progress;
progress.setMaximun(100);
Progress.setValue(100);

以上代码,对话框不会自动隐藏。 QProgressDialog有两种使用方式:模态和非模态。 与非模态QProgressDialog相比,模态QProgressDialog对于程序员来说更容易使用。在循环中执行操作,每隔一段时间调用setValue(),并使用wasCanceled()检查是否取消。 例如:

QProgressDialog progress("复制文件...", "中止", 0, numFiles, this);
      progress.setWindowModality(Qt::WindowModal);

      for (int i = 0; i < numFiles; i++) {
          progress.setValue(i);

          if (progress.wasCanceled())
              break;
          // 开始复制
	      // ......
      }
	   // 复制完成,将最大值设置给当前值,对话框自动隐藏,关闭
      progress.setValue(numFiles);

image.png

2 QProgressDialog常用函数

2.1 构造函数:

  • QProgressDialog(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()):创建一个默认的进度对话框,parent是对话框的父部件,f是对话框的窗口标志。
  • QProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()):创建一个带有文本标签、取消按钮和进度范围的进度对话框。labelText是标签的文本,cancelButtonText是取消按钮的文本,minimum和maximum指定了进度的范围。

2.2 成员函数:

  • void cancel():重置进度对话框,将wasCanceled()标记为true,直到进度对话框被重置。进度对话框将隐藏起来。
  • void canceled():在点击取消按钮时发射的信号,默认与cancel()槽连接。
  • void open(QObject *receiver, const char *member):打开对话框,并将其canceled()信号连接到receiver对象的member槽上。
  • void reset():重置进度对话框,如果autoClose()为true,则对话框将隐藏。
  • void setBar(QProgressBar *bar):设置进度条部件。
  • void setCancelButton(QPushButton *cancelButton):设置取消按钮部件。
  • void setCancelButtonText(const QString &cancelButtonText):设置取消按钮的文本。
  • void setLabel(QLabel *label):设置标签部件。
  • void setRange(int minimum, int maximum):设置进度范围。
  • QSize sizeHint() const:返回适合对话框内容的大小。

2.3 常用方法示例代码

CustomProgress::CustomProgress(QWidget *parent) : QProgressDialog(parent)
{
    // 设置窗体Flags, 对话框 | 只有关闭按钮,无问号
    this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
    // 设置取消按钮文本
    this->setCancelButtonText("取消");


    // 设置进度条区间,等同分别设置最小值  和  最大值
    setRange(0, 100);
    // 设置最小值
    setMinimum(0);
    // 设置最大值
    setMaximum(100);

    // 设置当前值
    setValue(0);

    // 设置文本,一般在进度条上方显示
    setLabelText("helloworld");
    
    // 设置自动关闭,当reset()时,对话框自动关闭,默认为true
    setAutoClose(true);
    // 设置自动重置,当reset()或当前值 == 最大值时,重置
    setAutoReset(true);


    connect(this, &CustomProgress::canceled, this, &CustomProgress::cancel);

    // 设置对话框窗体标题
    this->setWindowTitle("等待...");
}

3 线程Qthread

当在主线程里,进行耗时操作,然后加载进度对话框时,会发现,对话框进度条会卡死不动,然后耗时操作结束,对话框进度条直接关闭,界面感官不友好。 因此需要将耗时操作放到线程内操作,主线程就进行进度显示。界面也不会卡死。

QThread类提供了一种独立于平台的方式来管理线程。 QThread对象管理程序中的一个控制线程。QThreadsrun()中开始执行。默认情况下,run()通过调用exec()启动事件循环,并在线程内运行Qt事件循环。

此外,还可以通过使用QObject::moveToThread()将工作对象移动到线程中来使用它们。

下面是四种线程使用方式与进度框相结合,模拟处理耗时操作,主界面显示进度。

3.1 继承QThread,重写run()方法

.h

class C_Thread : public QThread
{
    Q_OBJECT
public:
    explicit C_Thread(int nMax, QObject *parent = nullptr);

    virtual void run() override;

signals:
    void emit_sendValue(int nValue);

private:
    int     m_nMax;
};

.cpp

#include "Thread.h"

C_Thread::C_Thread(int nMax, QObject *parent) : QThread(parent), m_nMax(nMax)
{

}

void C_Thread::run()
{
    int i = 0;
    while (i < m_nMax) {
        msleep(100);
        ++i;
        emit emit_sendValue(i);
    }
}

当进行start()时,自动调用run(),每过100ms,发送一个数据,主界面进行显示

3.2 继承QObject,之后添加到线程moveToThread(),使用信号和槽方式

需要将进行耗时操作的类,移动到线程中,之后以信号和槽的方式和进度框进行交互 .h

class C_ThreadObject : public QObject
{
    Q_OBJECT
public:
    explicit C_ThreadObject(int nMax, QObject *parent = nullptr);

signals:
    void emit_sendValue(int nValue);

public slots:
    void slot_dealValue();

private:
    int     m_nMax;
};

.cpp

C_ThreadObject::C_ThreadObject(int nMax, QObject *parent) : QObject(parent), m_nMax(nMax)
{

}

void C_ThreadObject::slot_dealValue()
{
    int i = 0;
    while (i < m_nMax) {
        QThread::msleep(100);
        ++i;
        emit emit_sendValue(i);
    }
}

3.3 继承QRunnable

重写run(),然后通过线程池调用 .h

class C_ThreadRunnable :public QObject, public QRunnable
{
    Q_OBJECT
public:
    explicit C_ThreadRunnable(int nMax);

signals:
    void emit_sendValue(int nValue);

public slots:

protected:
    virtual void run() override;

private:
    int m_nMax;
};

.cpp

C_ThreadRunnable::C_ThreadRunnable(int nMax) : m_nMax(nMax)
{

}

void C_ThreadRunnable::run()
{
    int i = 0;
    while (i < m_nMax) {
        QThread::msleep(100);
        ++i;
        emit emit_sendValue(i);
    }
}

3.4 使用QtConcurrent模块

Qt Concurrent模块扩展了Qt Core模块中的基本线程支持,简化了可以在所有可用CPU内核上并行执行的代码开发。 .h

void MainWindow::dealValue()
{
    int i = 0;
    while (i < m_nMax) {
        QThread::msleep(100);
        ++i;
        emit emit_sendValue(i);
    }
}

.cpp

QtConcurrent::run(this, &MainWindow::dealValue);

3.5 主线程调用

创建各个线程对象,然后进行模拟耗时操作,进度框显示进度 如果在构造中new过QProgressDialog对象,默认4秒后,会自动显示,可以调用其close()方法,不用显示。 .h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void dealValue();

signals:
    void emit_start();

    void emit_sendValue(int nValue);

private slots:
    void on_btn_TimeConsumingOperation_clicked();

private:
    Ui::MainWindow *ui;
    int                   m_nMax;

    C_DlgProgress*        m_pDlgProgress;
    C_Thread*             m_pTread;
    C_ThreadRunnable*     m_pThreadRunnable;

    C_ThreadObject*       m_pThreadObject;
    QThread*              m_pThreadUsedObject;
};

.cpp

#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);

    // 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);
}

3.6 结果

主界面显示: image.png 点击耗时操作image.png 当进度走完,进度框关闭。

4. 完整示例连接

完整链接

标签:nMax,对话框,int,void,用法,线程,MainWindow,emit,QThread
From: https://blog.51cto.com/u_16417016/9089417

相关文章

  • 语言中布尔值的用法和案例解析
    在C语言中,布尔值是一种用来表示真假的逻辑类型。它只能取两个值,即true和false。布尔值在C语言中的应用非常广泛,特别是在条件语句和循环语句中。在本文中,我们将通过一些案例来解析C语言中布尔值的用法。通过这些案例,我们可以更好地理解布尔值在实际编程中的应用。案例一:使用布尔值......
  • C++11中的匿名函数用法
    C++11中引用了匿名函数这一个新的特性,它的使用方法如下:[capture](parameters)->return_type{body} 其中:capture 指定了Lambda表达式可以访问的外部变量parameters 是Lambda表达式的参数列表return_type 是返回类型(可选)body 是Lambda函数体下面是一个简单......
  • 多线程
    NSThreadNSThread是iOS和macOS中用于多线程编程的类。它封装了线程的创建和管理,允许开发者在后台执行任务而不阻塞主线程。这样可以保持应用界面的响应性,同时执行如下载文件、数据处理等耗时操作。使用NSThread的常用方法和属性:detachNewThreadSelector:toTarget:withObject......
  • MySQL中CASE WHEN THEN用法
    MySQL中CASEWHENTHEN用于分类统计1、创建一个表createtableuser(   idintauto_incrementprimarykey,   agetinyintunsignednotnull); 2、添加一些数据insert intouser(age)values(12),(15),(20),(30),(35),(19),(24),(8),(61),(14);3、CASEWHENTHENselec......
  • netty: LengthFieldBasedFrameDecoder的用法示例
    一、服务器端启动类:packagecn.edu.tju;importio.netty.bootstrap.ServerBootstrap;importio.netty.buffer.ByteBuf;importio.netty.buffer.Unpooled;importio.netty.channel.*;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketCh......
  • Bmwgroupdesignworks爬虫,网站作品信息多线程采集爬虫源码!
    一个比较简单国外设计站点,作品信息采集爬虫源码,比较简单,采集的内容包括标题、内容及图片信息,适合新人学习参考使用。网站作品信息采集中,关于图片的采集下载使用了重试以及多线程的方式爬取采集下载,适合Python爬虫新人练手使用和学习,如果你正在找练手网站,不妨尝试爬取下载数据。三......
  • 数组指针的用法
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>//参数是数组形式voidprint1(intarr[3][5],intx,inty)//用数组形式接收,再接收传来的参数{ inti=0; intj=0; for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d",arr[i][j]);......
  • 《Java编程思想第四版》学习笔记51--关于线程组的控制
    //:ThreadGroup1.java//Howthreadgroupscontrolpriorities//ofthethreadsinsidethem.publicclassThreadGroup1{publicstaticvoidmain(String[]args){//Getthesystemthread&printitsInfo:ThreadGroupsys=Thread.currentThread().......
  • 线程
    theme:condensed-night-purplehighlight:a11y-dark线程理解线程的基础知识对于并发编程至关重要。以下是关于线程的一些基本概念:线程的定义:线程是操作系统能够进行调度的最小执行单位。一个进程可以包含多个线程,每个线程独立执行。线程的创建与启动:在多数编程语言中,可以通过调用......
  • 数据库超线程效果的一个验证
    数据库超线程效果的一个验证背景元旦加班期间,一直跟着同事再查一个项目的卡顿问题.自己想到了一个提高测试环境性能的方法.然后趁着元旦用的人少进行了一下验证.在业务空闲期间,批量进行Oracle数据库的统计信息更新动作.自己一开始担心的是如果数据量很大,执行时......