qt-13 进度条(模态和非模态)

progressdlg.h

#ifndef PROGRESSDLG_H
#define PROGRESSDLG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QProgressBar>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>

class progressDlg : public QDialog
{
    Q_OBJECT

public:
    progressDlg(QWidget *parent = nullptr);
    ~progressDlg();
private slots:
    void StartProgress();
private:
    QLabel* FileNum;
    QLineEdit* FileNumLineEdit;
    QLabel* ProgressType;
    QComboBox* ComboBox;
    QProgressBar* ProgressBar;
    QPushButton* StartBtn;
    QGridLayout* MainLayout;
};
#endif // PROGRESSDLG_H

progressdlg.cpp

#include "progressdlg.h"
#include <QProgressDialog>
#include <QFont>
progressDlg::progressDlg(QWidget *parent)
    : QDialog(parent)
{
    //实例化对象以及配置
    QFont font("ZYsong18030",12);
    setFont(font);
    setWindowTitle(tr("Progress"));
    FileNum = new QLabel(tr("文件数目"));
    FileNumLineEdit = new QLineEdit(tr("100000"));
    ProgressType = new QLabel("显示类型:");
    ComboBox = new QComboBox;
    ComboBox->addItem(tr("progressbar"));
    ComboBox->addItem(tr("progressDialog"));
    ProgressBar = new QProgressBar;
    StartBtn = new QPushButton(tr("开始"));
    //布局
    MainLayout = new QGridLayout(this);
    MainLayout->setMargin(15);
    MainLayout->setSpacing(10);
    MainLayout->addWidget(FileNum,0,0);
    MainLayout->addWidget(FileNumLineEdit,0,1);
    MainLayout->addWidget(ProgressType,1,0);
    MainLayout->addWidget(ComboBox,1,1);
    MainLayout->addWidget(ProgressBar,2,0,1,2);
    MainLayout->addWidget(StartBtn,3,1);
    //绑定事件
    connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartProgress()));


}

progressDlg::~progressDlg() {}

void progressDlg::StartProgress()
{
    bool Ok;
    int nNum = FileNumLineEdit->text().toInt(&Ok);
    if(ComboBox->currentIndex() ==0)
    {

        ProgressBar->setRange(0,nNum);
        nNum++;
        for(int i=0;i<nNum;i++)
        {
          ProgressBar->setValue(i);
        }
    }
    else if(ComboBox->currentIndex() == 1)
    {
        //创建一个进度对话框
        QProgressDialog* ProgressDialog = new QProgressDialog(this);
        QFont font("ZYSong18030",12);
        ProgressDialog->setFont(font);
        ProgressDialog->setWindowModality(Qt::WindowModal);
        ProgressDialog->setMinimumDuration(5);
        ProgressDialog->setWindowTitle(tr("please wait"));
        ProgressDialog->setLabelText(tr("Copy....."));
        ProgressDialog->setCancelButtonText(tr("cancel"));
        ProgressDialog->setRange(0,nNum);
        nNum++;

        for(int i =0;i<nNum;i++)
        {
            ProgressDialog->setValue(i);
            if(ProgressDialog->wasCanceled())
            {
                return;
            }
        }

    }


}

main.cpp

#include "progressdlg.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    progressDlg w;
    w.show();
    return a.exec();
}

运行图

模态

在这里插入图片描述

非模态

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值