进度条-模态和非模态
progressdlg.h progressdlg.cpp main.cpp 运行图
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.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 ( ) ;
}
运行图
模态
非模态