Qt learning-progress bar qprogressbar/qprogressdialog class

Source: Internet
Author: User

Two progress bars are displayed:

Qprogressbar and qprogressdialog

The following example shows two progress bars:

Code:

In the header file dialog. h:

#include <QtGui/QDialog>#include <QLabel>#include <QLineEdit>#include <QProgressBar>#include <QComboBox>#include <QPushButton>#include <QGridLayout>class Dialog : public QDialog{    Q_OBJECTpublic:    Dialog(QWidget *parent = 0);    ~Dialog();private slots:    void startProgress();private:    QLabel *FileNum;    QLineEdit *FileNumLineEdit;    QLabel *ProgressType;    QComboBox *comboBox;    QProgressBar *progressBar;    QPushButton *startBtn;    QGridLayout *mainLayout;};

Source file:

# Include "dialog. H "# include <qprogressdialog> # include <qfont> dialog: Dialog (qwidget * parent): qdialog (parent) {setwindowtitle (TR (" progress ")); filenum = new qlabel; filenum-> settext (TR ("number of files:"); filenumlineedit = new qlineedit; filenumlineedit-> settext (TR ("10000 ")); progresstype = new qlabel; progresstype-> settext (TR ("display type:"); ComboBox = new qcombobox; ComboBox-> additem (TR ("progressbar ")); comboBox-> additem (TR ("progressdialog"); progressbar = new qprogressbar; startbtn = new qpushbutton; startbtn-> settext (TR (" ")); mainlayout = new qgridlayout (this); mainlayout-> addwidget (filenum,); mainlayout-> addwidget (filenumlineedit,); mainlayout-> addwidget (progresstype ); mainlayout-> addwidget (ComboBox,); mainlayout-> addwidget (progressbar,); mainlayout-> addwidget (startbtn,); mainlayout-> setmargin (15 ); mainlayout-> setspacing (10); Connect (startbtn, signal (clicked (), this, slot (startprogress ()))}
Void dialog: startprogress () {bool OK; int num = filenumlineedit-> text (). toint (& OK); If (! OK) return; If (ComboBox-> currentindex () = 0) {progressbar-> setrange (0, num); For (INT I = 1; I <= num; I ++) {progressbar-> setvalue (I) ;}} else if (ComboBox-> currentindex () = 1) {qprogressdialog * progressdlg = new qprogressdialog (this ); qfont font ("zysong18030", 12); progressdlg-> setfont (font); progressdlg-> setwindowmodality (QT: windowmodal); progressdlg-> setminimumduration (5 ); progressdlg-> setwindowtitle (TR ("Please wait"); progressdlg-> setlabeltext (TR ("copying ...... "); progressdlg-> setcancelbuttontext (TR (" canceled "); progressdlg-> setrange (0, num); For (INT I = 1; I <= num; I ++) {progressdlg-> setvalue (I); If (progressdlg-> wascanceled () return ;}}}

The main function adds the code to display Chinese characters:

#include <QtGui/QApplication>#include "dialog.h"#include <QTextCodec>int main(int argc, char *argv[]){    QApplication a(argc, argv);    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());    Dialog w;    w.show();    return a.exec();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.