• 为了保证你在浏览本网站时有着更好的体验,建议使用类似Chrome、Firefox之类的浏览器~~
    • 如果你喜欢本站的内容何不Ctrl+D收藏一下呢,与大家一起分享各种编程知识~
    • 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此,生命在于折腾,何不年轻时多折腾一下

Qt对话框之进度条

Qt admin 4年前 (2016-03-24) 2632次浏览 0个评论 扫描二维码

博主开始利用业余时间学习 QT 了,完成学习写界面的伟大理想。。。。。。。Qt 初步学习到对话框,晚上看着给的截面图尝试自己写代码完成界面。。。。

简单介绍下:

Qt 进度条有两种

1、QProgressBar

2、QProgressDialog

下面给出代码完成这两种进度条的实现:

 

 

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QLabel>
#include<QLineEdit>
#include<QProgressBar>
#include<QPushButton>
#include<QComboBox>
#include<QGridLayout>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private slots:
    void showprogress();
private:
    QLabel *filename;
    QLineEdit *filenum;
    QLabel *showlabel;
    QComboBox *showshow;
    QProgressBar * progress;
    QPushButton *start;
    QGridLayout *mainlayout;

};

#endif // DIALOG_H

#include "dialog.h"
#include<QProgressDialog>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{

    setWindowTitle(tr("Progress"));
    QFont fontdefine("ZYSong18030",12);
    setFont(fontdefine);
    filename=new QLabel;
    filename->setText(tr("文件数目:"));
    filenum=new QLineEdit ;
    filenum->setText(tr("10000"));
    showlabel=new QLabel;
    showlabel->setText(tr("显示类型:"));
    showshow=new QComboBox;
    showshow->addItem(tr("progressbar"));
    showshow->addItem(tr("progressdialog"));
    progress=new QProgressBar;
    start=new QPushButton;
    start->setText(tr("开始"));
    mainlayout=new QGridLayout(this);
    mainlayout->addWidget(filename,0,0);
    mainlayout->addWidget(filenum,0,1);
    mainlayout->addWidget(showlabel,1,0);
    mainlayout->addWidget(showshow,1,1);
    mainlayout->addWidget(progress,2,0,1,2);
    mainlayout->addWidget(start,3,1);
    mainlayout->setMargin(15);
    mainlayout->setSpacing(10);
    connect(start,SIGNAL(clicked()),this,SLOT(showprogress()));


}
void Dialog::showprogress()
{
    bool ok;
    int num=filenum->text().toInt(&ok);
    if (showshow->currentIndex()==0)
    {
        progress->setRange(0,num);
        for(int i=0;i<num+1;i++)
        {
            progress->setValue(i);
        }

    }else if(showshow->currentIndex()==1)
    {
       QProgressDialog *progressdialog=new QProgressDialog(this);
       QFont fontdefine("ZYSong18030",12);
       setFont(fontdefine);
       progressdialog->setWindowModality(Qt::WindowModal);
       progressdialog->setMinimumDuration(5);
       progressdialog->setWindowTitle(tr("等待"));
       progressdialog->setLabelText(tr("copy..."));
       progressdialog->setCancelButton(new QPushButton(tr("cancel")));
       progressdialog->setRange(0,num);
       for(int i=0;i<num+1;i++)
       {
           progressdialog->setValue(i);
           if(progressdialog->wasCanceled())
               return;
       }


    }
}

Dialog::~Dialog()
{

}

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}


Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Qt 对话框之进度条
喜欢 (1)
admin
关于作者:
互联网行业码农一枚/业余铲屎官/数码影音爱好者/二次元

您必须 登录 才能发表评论!