Qt 학습-진도표 QProgressBar/QProgressDialog 클래스

3297 단어 Meego
두 가지 진행률 표시줄 표시:
QProgressBar 및 QProgressDialog
다음 예에서는 두 가지 진행률 막대를 보여 줍니다.
     
코드:
헤더 파일 dialog.h중:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private slots:
    void startProgress();
private:
    QLabel *FileNum;
    QLineEdit *FileNumLineEdit;
    QLabel *ProgressType;
    QComboBox *comboBox;
    QProgressBar *progressBar;
    QPushButton *startBtn;
    QGridLayout *mainLayout;
};

소스 파일:
#include "dialog.h"
#include 
#include 

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Progress"));

    FileNum=new QLabel;
    FileNum->setText(tr("    :"));
    FileNumLineEdit=new QLineEdit;
    FileNumLineEdit->setText(tr("10000"));

    ProgressType=new QLabel;
    ProgressType->setText(tr("    :"));
    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,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);
    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("    ......      "));
        progressDlg->setCancelButtonText(tr("  "));
        progressDlg->setRange(0,num);
        for(int i=1;i<=num;i++)
        {
            progressDlg->setValue(i);
            if(progressDlg->wasCanceled())
                return;
        }
    }
}

main 함수에 중국어를 표시하는 코드를 추가하려면:
#include 
#include "dialog.h"
#include 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    Dialog w;
    w.show();

    return a.exec();
}

좋은 웹페이지 즐겨찾기