QT中progress的使用

简介:

progress。h

#ifndef PROGRESS_H
#define PROGRESS_H

#include <QtGui>
#include <QtTest>
#include "ui_progress.h"

class progress : public QDialog
{
	Q_OBJECT

public:
	progress(QWidget *parent = 0, Qt::WFlags flags = 0);
	~progress();

	QLabel *numLabel; 
	QLineEdit *numLineEdit;
	QLabel *typeLabel; 
	QComboBox *typeComboBox;

	QProgressBar *progressBar;
	QPushButton *startPushButton;

private:
	Ui::progressClass ui;
	private slots:
		void slotStart();
};

#endif // PROGRESS_H

progress.cpp

#include "progress.h"

progress::progress(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);
	setWindowTitle(tr("Progress"));

	QGridLayout *layout = new QGridLayout( this );  

	numLabel = new QLabel(tr("File Num:")); 
	numLineEdit =  new QLineEdit;
	numLineEdit->setText(tr("10"));

	typeLabel = new QLabel(tr("Progress Type:"));
	typeComboBox = new QComboBox;
	typeComboBox->addItem(tr("ProgressBar"));
	typeComboBox->addItem(tr("ProgressDialog"));

	progressBar = new QProgressBar;

	startPushButton = new QPushButton(tr("Start"));

	layout->addWidget( numLabel, 0, 0 );	
	layout->addWidget( numLineEdit, 0, 1 );
	layout->addWidget( typeLabel, 1, 0 );
	layout->addWidget( typeComboBox, 1, 1 );
	layout->addWidget( progressBar, 2, 0, 1, 2 );
	layout->addWidget( startPushButton, 3, 1 );
	layout->setMargin(15);
	layout->setSpacing(10);

	connect(startPushButton,SIGNAL(clicked()),this,SLOT(slotStart()));
}

progress::~progress()
{

}

void progress::slotStart()
{
	int num=numLineEdit->text().toInt();

	if(typeComboBox->currentIndex() == 0)/*ProgressBar*/
	{
		progressBar->setRange(0,num);
		for (int i=1; i<num+1; i++)
		{
			progressBar->setValue(i);
			/*此处模拟文件拷贝过程*/
			QTest::qWait(1000);
		}		
	}
	else if(typeComboBox->currentIndex() == 1)/*modal ProgressDialog*/
	{
		QProgressDialog *progressDialog = new QProgressDialog(this);		
		progressDialog->setWindowModality(Qt::WindowModal);

		progressDialog->setMinimumDuration(5);
		progressDialog->setWindowTitle(tr("Please Wait"));
		progressDialog->setLabelText(tr("Copying..."));
		progressDialog->setCancelButtonText(tr("Cancel"));		
		progressDialog->setRange(0,num);

		for (int i=1; i<num+1; i++)
		{
			progressDialog->setValue(i);
			qApp->processEvents();
			/*此处模拟文件拷贝过程*/
			QTest::qWait(1000);    	    
			if (progressDialog->wasCanceled())
				return;
		}			
	}

}

main.cpp

#include "progress.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	progress *w=new progress;
	w->show();
	return a.exec();
}
 

imageimage

目录
相关文章
25 QT - event函数
25 QT - event函数
58 0
|
7月前
【qt】Tool Box组件
【qt】Tool Box组件
64 0
|
4月前
【Qt 学习笔记】Qt常用控件 | 显示类控件 | Progress Bar的使用及说明
【Qt 学习笔记】Qt常用控件 | 显示类控件 | Progress Bar的使用及说明
466 0
|
7月前
|
Android开发
Can t process attribute android:fillColor=@color/camera_progress_delete: references to other resou
Can t process attribute android:fillColor=@color/camera_progress_delete: references to other resou
46 1
|
数据可视化 Python
进度条Progress的用法介绍
进度条Progress的用法介绍
212 0
|
JavaScript 前端开发 测试技术
UI 提供的【progress-step】要🙉怎么🙊实现!!!
UI 提供的【progress-step】要🙉怎么🙊实现!!!
93 0
Qt 编译时遇到 error: [debug/qrc_music.cpp] Error 1
第一种方式,清理编译文件 把Qdebug release 文件件都删除, 删除makefile 文件 尝试重新编译 以上是网上寻找的结果 以下是我的解决方法
685 0
|
JavaScript 前端开发 容器