博主开始利用业余时间学习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(); }