实例.进程条
这个必须要在.pro文件下加入:
QT += testlib 这样才能解决这样的bug。error: collect2: ld returned 1 exit status
progress.h
#ifndef PROGRESS_H
#define PROGRESS_H
#include <QtGui>
class Progress:public QDialog
{
Q_OBJECT
public:
Progress(QWidget *parent=0, Qt::WindowFlags f=0);
~Progress();
public:
QLabel *m_pLabelNum;
QLineEdit *m_pLineEditNum;
QLabel *m_pLabelType;
QComboBox *m_pComboBoxType;
QProgressBar *m_pProgressBar;
QPushButton *m_pPBStart;
private slots:
void slotPBStart();
};
#endif // PROGRESS_H
progress.cpp
#include "progress.h"
#include <QtTest/QTest> // To link against the module, add this line to your qmake .pro file:CONFIG += qtestlib
Progress::Progress(QWidget *parent, Qt::WindowFlags f):QDialog(parent, f)
{
setWindowTitle(tr("Progress")); // 设置窗口标题
QGridLayout *pGLayout = new QGridLayout(this); // 表格布局控件
m_pLabelNum = new QLabel(tr("File Number:"));
m_pLineEditNum = new QLineEdit;
m_pLineEditNum->setText(tr("10"));
m_pLabelType = new QLabel(tr("Progress Type:"));
m_pComboBoxType = new QComboBox;
m_pComboBoxType->addItem(tr("ProgressBar"));
m_pComboBoxType->addItem(tr("ProgressDialog"));
m_pProgressBar = new QProgressBar;
m_pProgressBar->setFormat("%p%, %v/%m"); // 设置进度条上文本样式 ,默认%p%, %p 百分比, %v 当前值,%m 总的步进值
m_pPBStart = new QPushButton(tr("Start"));
pGLayout->addWidget( m_pLabelNum, 0, 0 );
pGLayout->addWidget( m_pLineEditNum, 0, 1 );
pGLayout->addWidget( m_pLabelType, 1, 0 );
pGLayout->addWidget( m_pComboBoxType, 1, 1 );
pGLayout->addWidget( m_pProgressBar, 2, 0, 1, 2 ); // 第三行,第一列,占一行,占二列
pGLayout->addWidget( m_pPBStart, 3, 1 );
pGLayout->setMargin(15); // 设置布局四边距
pGLayout->setSpacing(10); // 设置布局内部单元格边距
connect(m_pPBStart, SIGNAL(clicked()), this, SLOT(slotPBStart())); // 绑定按钮点击事件处理函数
}
Progress::~Progress()
{
}
void Progress::slotPBStart()
{
int nNum = m_pLineEditNum->text().toInt(); // 文本框中整数值
if(m_pComboBoxType->currentIndex() == 0)
{ // ProgressBar 进度条
m_pProgressBar->setRange(0, nNum); // 设置进度条大小值
for (int i=1; i<nNum+1; i++)
{
m_pProgressBar->setValue(i);
QTest::qWait(1000); // 等待1秒
}
}
else if(m_pComboBoxType->currentIndex() == 1)
{ // modal ProgressDialog 模态进度对话框
QProgressDialog *pProgressDialog = new QProgressDialog(this);
pProgressDialog->setWindowModality(Qt::WindowModal); // 设置为模态窗口
pProgressDialog->setMinimumDuration(5); // 设置最小持续时间
pProgressDialog->setWindowTitle(tr("Please Wait")); //设置进度对话框中
pProgressDialog->setLabelText(tr("Copying...")); // 设置显示标签内容
pProgressDialog->setCancelButtonText(tr("Cancel"));// 设置取消按钮内容
pProgressDialog->setRange(0, nNum); // 设置进度对话框中进度条大小值
for (int i=1; i<nNum+1; i++)
{
pProgressDialog->setValue(i);
qApp->processEvents(); // qApp 全局程序对象指针, qApp->processEvents() 来正常响应事件循环,确保应用程序不会出现阻塞
QTest::qWait(1000); // 等待延时多少毫秒
if (pProgressDialog->wasCanceled()) // 检测进度对话框中”取消“按钮是否被触发。
return;
}
}
}
main.cpp
#include<QApplication> // 所有QT图形化应用程序必须包含此文件,它包含了QT图形化应用程序的各种资源、基本设置、控制流及事件处理等。
#include"progress.h" // 自定义类头文件
intmain(int argc, char *argv[])
{
QApplicationapp(argc, argv);
Progress*pProgress = new Progress();
pProgress->show();
returnapp.exec();
}