Progress Bar
[toc align_right="yes" depth="3"]
Progress Bar
It is often necessary to display a progress bar while a long operation is happening. The case we are concerned about in this example is when there is no easy way to track the progress of the operation - all that is known is when it is done. There are many ways to do this. You could use a progressBar widget on your widget and run the operation in a different thread (using moveToThread()). This typically requires a special object to be created (a subclass of "QObject":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html that runs the operation and then emits a finished() signal), which can be a pain if you need to do this for many different operations.
However, using "QFutureWatcher":http://developer.qt.nokia.com/doc/qt-4.8/qfuturewatcher.html and "QtConcurrent::run()":http://developer.qt.nokia.com/doc/qt-4.8/qtconcurrentrun.html#run, this is extremely easy. Below we demonstrate how to use this technique with both a QProgressDialog and a QProgressBar.
QProgressBar Example
form.h
#ifndef FORM_H<br />#define FORM_H
#include "ui_form.h&quot;
#include <QFutureWatcher&gt;
#include "MyClass.h&quot;
class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
public slots:
void slot_finished();<br /> void on_pushButton_clicked();
public:<br /> Form(QWidget *parent = 0);
private:<br /> QFutureWatcher&lt;void&gt; FutureWatcher;<br /> MyClass MyObject;<br />};
#endif<br />
form.cpp
<br />#include <QtGui&gt;<br />#include <QImage&gt;
#include "form.h&quot;
#include <iostream&gt;
Form::Form(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
this->progressBar->setMinimum(0);<br /> this->progressBar->setMaximum(0);<br /> this->progressBar->hide();
connect(&this->FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));
}
void Form::slot_finished()<br />{<br /> this->progressBar->hide();<br />}
void Form::on_pushButton_clicked()<br />{<br /> this->progressBar->show();
// Start the computation.<br /> QFuture&lt;void&gt; future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);<br /> this->FutureWatcher.setFuture(future);<br />}<br />
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
FutureWatcher.cpp
#include <QApplication&gt;<br />#include <QObject&gt;<br />#include <QThread&gt;
#include <iostream&gt;
#include "form.h&quot;
int main(int argc, char*argv[])<br />{<br /> QApplication app(argc, argv);
Form form;
form.show();
return app.exec&amp;#40;&#41;;<br />}
MyClass.h
<br />#ifndef MyClass_H<br />#define MyClass_H
#include <iostream&gt;
class MyClass<br />{<br />public:
void LongFunction()<br /> {<br /> for( int count = 0; count < 5; count++ )<br /> {<br /> sleep( 1 );<br /> std::cout << "Ping long!" << std::endl;<br /> }<br /> }<br />};
#endif<br />
CMakeLists.txt
<br />cmake_minimum_required(VERSION 2.6)
PROJECT (FutureWatcher)
FIND_PACKAGE(Qt4 REQUIRED)<br />INCLUDE ($&#123;QT_USE_FILE&#125;)
QT4_WRAP_CPP(MOCSrcs form.h)<br />QT4_WRAP_UI(UISrcs form.ui)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(FutureWatcher FutureWatcher.cpp MyClass.cpp form.cpp ${MOCSrcs} ${UISrcs})<br />TARGET_LINK_LIBRARIES(FutureWatcher ${QT_LIBRARIES})<br />
QProgressDialog Example
form.h
<br />#ifndef FORM_H<br />#define FORM_H
#include "ui_form.h&quot;
#include <QFutureWatcher&gt;
#include "MyClass.h&quot;
class QProgressDialog;
class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
public slots:
void slot_finished();<br /> void on_pushButton_clicked();
public:<br /> Form(QWidget '''parent = 0);
<br />private:<br /> QFutureWatcher&lt;void&gt; FutureWatcher;<br /> MyClass MyObject;<br /> QProgressDialog''' ProgressDialog;<br />};
#endif<br />
form.cpp
<br />#include <QtGui&gt;<br />#include <QImage&gt;
#include "form.h&quot;
#include <iostream&gt;
Form::Form(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
this->ProgressDialog = new QProgressDialog(this);
connect(&this->FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));<br /> connect(&this->FutureWatcher, SIGNAL (finished()), this->ProgressDialog , SLOT (cancel()));
}
void Form::slot_finished()<br />{<br /> std::cout << "Finshed&quot; << std::endl;<br />}
void Form::on_pushButton_clicked()<br />{<br /> // Start the computation.<br /> QFuture&lt;void&gt; future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);<br /> this->FutureWatcher.setFuture(future);
this->ProgressDialog->setMinimum(0);<br /> this->ProgressDialog->setMaximum(0);<br /> this->ProgressDialog->setWindowModality(Qt::WindowModal);<br /> this->ProgressDialog->exec&amp;#40;&#41;;
}<br />
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
MyClass.h
<br />#ifndef MyClass_H<br />#define MyClass_H
#include <iostream&gt;
class MyClass<br />{<br />public:
void LongFunction()<br /> {<br /> for( int count = 0; count < 5; count++ )<br /> {<br /> sleep( 1 );<br /> std::cout << "Ping long!" << std::endl;<br /> }<br /> }<br />};
#endif<br />
FutureWatcherProgressDialog.cpp
#include <QApplication>
#include <QObject>
#include <QThread>
- include <iostream>
- include "form.h"
int main(int argc, char*argv[])
{
QApplication app(argc, argv);
Form form;
form.show();
return app.exec&#40;);