Progress Bar

From Qt Wiki
Revision as of 14:22, 23 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search



[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 &quot;ui_form.h&amp;quot;

#include &lt;QFutureWatcher&amp;gt;

#include &quot;MyClass.h&amp;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&amp;lt;void&amp;gt; FutureWatcher;<br /> MyClass MyObject;<br />};

#endif<br />

form.cpp

<br />#include &lt;QtGui&amp;gt;<br />#include &lt;QImage&amp;gt;

#include &quot;form.h&amp;quot;

#include &lt;iostream&amp;gt;

Form::Form(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);

this-&gt;progressBar-&gt;setMinimum(0);<br /> this-&gt;progressBar-&gt;setMaximum(0);<br /> this-&gt;progressBar-&gt;hide();

connect(&amp;this-&gt;FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));

}

void Form::slot_finished()<br />{<br /> this-&gt;progressBar-&gt;hide();<br />}

void Form::on_pushButton_clicked()<br />{<br /> this-&gt;progressBar-&gt;show();

// Start the computation.<br /> QFuture&amp;lt;void&amp;gt; future = QtConcurrent::run(&amp;this-&gt;MyObject, &amp;MyClass::LongFunction);<br /> this-&gt;FutureWatcher.setFuture(future);<br />}<br />

form.ui

<br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;Form&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;Form&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;400&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;300&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;Form&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;layout class=&quot;QVBoxLayout&amp;quot; name=&quot;verticalLayout&amp;quot;&gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QPushButton&amp;quot; name=&quot;pushButton&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;PushButton&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QLineEdit&amp;quot; name=&quot;lineEdit&amp;quot;/&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QProgressBar&amp;quot; name=&quot;progressBar&amp;quot;&gt;<br /> &lt;property name=&quot;value&amp;quot;&gt;<br /> &lt;number&amp;gt;24&amp;lt;/number&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;<br />

FutureWatcher.cpp

#include &lt;QApplication&amp;gt;<br />#include &lt;QObject&amp;gt;<br />#include &lt;QThread&amp;gt;

#include &lt;iostream&amp;gt;

#include &quot;form.h&amp;quot;

int main(int argc, char*argv[])<br />{<br /> QApplication app(argc, argv);

Form form;

form.show();

return app.exec&amp;amp;#40;&amp;#41;;<br />}

MyClass.h

<br />#ifndef MyClass_H<br />#define MyClass_H

#include &lt;iostream&amp;gt;

class MyClass<br />{<br />public:

void LongFunction()<br /> {<br /> for( int count = 0; count &lt; 5; count++ )<br /> {<br /> sleep( 1 );<br /> std::cout &lt;&lt; &quot;Ping long!&quot; &lt;&lt; std::endl;<br /> }<br /> }<br />};

#endif<br />

CMakeLists.txt

<br />cmake_minimum_required(VERSION 2.6)

PROJECT (FutureWatcher)

FIND_PACKAGE(Qt4 REQUIRED)<br />INCLUDE ($&amp;#123;QT_USE_FILE&amp;#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 &quot;ui_form.h&amp;quot;

#include &lt;QFutureWatcher&amp;gt;

#include &quot;MyClass.h&amp;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&amp;lt;void&amp;gt; FutureWatcher;<br /> MyClass MyObject;<br /> QProgressDialog''' ProgressDialog;<br />};

#endif<br />

form.cpp

<br />#include &lt;QtGui&amp;gt;<br />#include &lt;QImage&amp;gt;

#include &quot;form.h&amp;quot;

#include &lt;iostream&amp;gt;

Form::Form(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);

this-&gt;ProgressDialog = new QProgressDialog(this);

connect(&amp;this-&gt;FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));<br /> connect(&amp;this-&gt;FutureWatcher, SIGNAL (finished()), this-&gt;ProgressDialog , SLOT (cancel()));

}

void Form::slot_finished()<br />{<br /> std::cout &lt;&lt; &quot;Finshed&amp;quot; &lt;&lt; std::endl;<br />}

void Form::on_pushButton_clicked()<br />{<br /> // Start the computation.<br /> QFuture&amp;lt;void&amp;gt; future = QtConcurrent::run(&amp;this-&gt;MyObject, &amp;MyClass::LongFunction);<br /> this-&gt;FutureWatcher.setFuture(future);

this-&gt;ProgressDialog-&gt;setMinimum(0);<br /> this-&gt;ProgressDialog-&gt;setMaximum(0);<br /> this-&gt;ProgressDialog-&gt;setWindowModality(Qt::WindowModal);<br /> this-&gt;ProgressDialog-&gt;exec&amp;amp;#40;&amp;#41;;

}<br />

form.ui

<br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;Form&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;Form&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;400&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;300&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;Form&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;layout class=&quot;QVBoxLayout&amp;quot; name=&quot;verticalLayout&amp;quot;&gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QPushButton&amp;quot; name=&quot;pushButton&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;PushButton&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;<br />

MyClass.h

<br />#ifndef MyClass_H<br />#define MyClass_H

#include &lt;iostream&amp;gt;

class MyClass<br />{<br />public:

void LongFunction()<br /> {<br /> for( int count = 0; count &lt; 5; count++ )<br /> {<br /> sleep( 1 );<br /> std::cout &lt;&lt; &quot;Ping long!&quot; &lt;&lt; std::endl;<br /> }<br /> }<br />};

#endif<br />

FutureWatcherProgressDialog.cpp

#include <QApplication&gt;
#include <QObject&gt;
#include <QThread&gt;

  1. include <iostream&gt;
  1. include "form.h&quot;

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

Form form;

form.show();

return app.exec&amp;#40;&#41;;