Jump to content

Progress Bar

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



[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


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

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


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

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;;