본문 바로가기
SWE/Qt

Qt예제] QProgressBar로 진행 상태 위젯 만들기 (%표현)

by S나라라2 2019. 8. 20.
반응형

QProgressBar

: 진행상황을 보여주는 위젯

FTP, HTTP와 같은 인터넷 애플리케이션을 작성할 때 유용

ex) 파일을 다운로드, 프로그램 설치할 때 유용

 

 

<QProgressBar를 이용해 진행 상태 위젯 만들기>

 

예제 결과

예제 결과
start 버튼 클릭시
stop버튼 클릭시

 

// mywidget.h
// QProgressBar
// : 진행상황 보여주는 위젯
// ex) 파일을 다운로드, 프로그램 설치할 때 유용

// 진행 상태를 % 단위로 표기하려면
// 최소값과 최대값을 설정해야 한다.


/* -- QProgressBar를 이용해 진행 상태 위젯 만들기 (%표현) --*/

#include <QDialog>

class QProgressBar;
class QPushButton;
class QHBoxLayout;

class MyWidget : public QDialog
{
	Q_OBJECT

	public :
		MyWidget();

	private slots :
		void start();
		void stop();
		void update();

	private :
		QProgressBar *progress1;
		QPushButton *startButton;
		QPushButton *stopButton;
		QTimer *timer;

		qint64 value;

		QHBoxLayout *layout;
};

 

//mywidget.cpp
#include <QtGui>
#include "mywidget.h"

MyWidget::MyWidget()
{
	setFixedSize(500,200);

	// QProgressBar
	progress1 = new QProgressBar();
	progress1->setMaximum(100);  // 최초 최대값 설정-> 자동으로 진행 상항을 퍼센트로 보여준다.
	progress1->setValue(0);      // 현재 QProgressBar위젯의 설정값을 할당할 수 있다.
	value = 0;
	// *추가 공부*
	// void QProgressBar::setRange(int minimum, int maximum)
	// QProgressBar위젯의 최소값과 최대값을 한 번에 정할 수 있음
	// void setInvertedAppearance (bool invert)
	// 오른쪽에서 왼쪽으로 진행 상황 -> 파라미터 true 설정


	// QTimer위젯에 의해 0.1초 간격으로
	// QProgressBar위젯의 설정값이 1씩 증가한다.
	timer = new QTimer(this);
	connect (timer, SIGNAL(timeout()), this, SLOT(update()));


	// start버튼 : QProgressBar 위젯의 진행 상황을 퍼센트로 표현
	startButton = new QPushButton(tr("Start"));
	// stop버튼 : QTimer 위젯을 정지
	stopButton = new QPushButton(tr("Stop"));

	connect(startButton, SIGNAL(clicked()),this, SLOT(start()));
	connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));

	layout = new QHBoxLayout;
	layout->addWidget(progress1);
	layout->addWidget(startButton);
	layout->addWidget(stopButton);

	setLayout(layout);
}

// start버튼 클릭 시 실행되는 slot함수
void MyWidget::start(){

	startButton->setEnabled(false);
	timer->start(100);
}

// stop버튼 클릭 시 실행되는 slot함수
void MyWidget::stop()
{
	startButton->setEnabled(true);
	timer->stop();
}

void MyWidget::update()
{
	value += 1;
	progress1->setValue(value);
}

 

//main.cpp
#include <QApplication>
#include <QStyleFactory>

#include "mywidget.h"

int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
	MyWidget widget;

	QStringList styles = QStyleFactory::keys();
	app.setStyle(styles[3]);

	widget.show();
	return app.exec();
}

 

 

반응형