Progress Bar: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]] | |||
[toc align_right="yes&quot; depth="3&quot;] | |||
= 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&quot;: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&quot;: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. | === form.h === | ||
<code>#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 /></code> | |||
* | === form.cpp === | ||
* [ | |||
* | <code><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 /></code> | |||
=== form.ui === | |||
<code><br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Form&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Form&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Form&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;PushButton&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit&quot;/&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QProgressBar&quot; name="progressBar&quot;><br /> <property name="value&quot;><br /> <number&gt;24&lt;/number&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt;<br /></code> | |||
=== FutureWatcher.cpp === | |||
<code>#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 />}</code> | |||
=== MyClass.h === | |||
<code><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 /></code> | |||
=== CMakeLists.txt === | |||
<code><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 /></code> | |||
== QProgressDialog Example == | |||
=== form.h === | |||
<code><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 /></code> | |||
=== form.cpp === | |||
<code><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 /></code> | |||
=== form.ui === | |||
<code><br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Form&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Form&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Form&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;PushButton&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt;<br /></code> | |||
MyClass.h<br /><code><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 /></code> | |||
=== FutureWatcherProgressDialog.cpp === | |||
<code>#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;; |
Revision as of 14:22, 23 February 2015
[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
<br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Form&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Form&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Form&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;PushButton&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit&quot;/&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QProgressBar&quot; name="progressBar&quot;><br /> <property name="value&quot;><br /> <number&gt;24&lt;/number&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt;<br />
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
<br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Form&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Form&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Form&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;PushButton&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt;<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 />
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;);