Progress Bar: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
{{LangSwitch}}
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
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 [http://doc.qt.io/qt-5/qobject.html QObject] 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.


[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
However, using [http://doc.qt.io/qt-5/qfuturewatcher.html QFutureWatcher] and [http://doc.qt.io/qt-5/qtconcurrentrun.html QtConcurrent::run()], this is extremely easy. Below we demonstrate how to use this technique with both a [http://doc.qt.io/qt-5/qprogressdialog.html QProgressDialog] and a [http://doc.qt.io/qt-5/qprogressbar.html QProgressBar].
 
= 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 &quot;QObject&amp;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 &quot;QFutureWatcher&amp;quot;:http://developer.qt.nokia.com/doc/qt-4.8/qfuturewatcher.html and &quot;QtConcurrent::run()&quot;: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 ==
== QProgressBar Example ==
Line 13: Line 11:
=== form.h ===
=== form.h ===


<code>#ifndef FORM_H<br />#define FORM_H
<code>#ifndef FORM_H
#define FORM_H


#include &quot;ui_form.h&amp;quot;
#include "ui_form.h"


#include &lt;QFutureWatcher&amp;gt;
#include <QFutureWatcher>


#include &quot;MyClass.h&amp;quot;
#include "MyClass.h"


class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
class Form : public QWidget, private Ui::Form
{
Q_OBJECT


public slots:
public slots:


void slot_finished();<br /> void on_pushButton_clicked();
void slot_finished();
void on_pushButton_clicked();


public:<br /> Form(QWidget *parent = 0);
public:
Form(QWidget *parent = 0);


private:<br /> QFutureWatcher&amp;lt;void&amp;gt; FutureWatcher;<br /> MyClass MyObject;<br />};
private:
QFutureWatcher<void> FutureWatcher;
MyClass MyObject;
};


#endif<br /></code>
#endif
</code>


=== form.cpp ===
=== form.cpp ===


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


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


#include &lt;iostream&amp;gt;
#include <iostream>


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


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


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


}
}


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


void Form::on_pushButton_clicked()<br />{<br /> this-&gt;progressBar-&gt;show();
void Form::on_pushButton_clicked()
{
this->progressBar->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 /></code>
// Start the computation.
QFuture<void> future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);
this->FutureWatcher.setFuture(future);
}
</code>


=== form.ui ===
=== form.ui ===


<code><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 /></code>
<code>
<?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>
</code>


=== FutureWatcher.cpp ===
=== FutureWatcher.cpp ===


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


#include &lt;iostream&amp;gt;
#include <iostream>


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


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


Form form;
Form form;
Line 73: Line 139:
form.show();
form.show();


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


=== MyClass.h ===
=== MyClass.h ===


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


#include &lt;iostream&amp;gt;
#include <iostream>


class MyClass<br />{<br />public:
class MyClass
{
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 />};
void LongFunction()
{
for( int count = 0; count < 5; count++ )
{
sleep( 1 );
std::cout << "Ping long!" << std::endl;
}
}
};


#endif<br /></code>
#endif
</code>


=== CMakeLists.txt ===
=== CMakeLists.txt ===


<code><br />cmake_minimum_required(VERSION 2.6)
<code>
cmake_minimum_required(VERSION 2.6)


PROJECT (FutureWatcher)
PROJECT (FutureWatcher)


FIND_PACKAGE(Qt4 REQUIRED)<br />INCLUDE ($&amp;#123;QT_USE_FILE&amp;#125;)
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE (${QT_USE_FILE})


QT4_WRAP_CPP(MOCSrcs form.h)<br />QT4_WRAP_UI(UISrcs form.ui)
QT4_WRAP_CPP(MOCSrcs form.h)
QT4_WRAP_UI(UISrcs form.ui)


include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
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>
ADD_EXECUTABLE(FutureWatcher FutureWatcher.cpp MyClass.cpp form.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(FutureWatcher ${QT_LIBRARIES})
</code>


== QProgressDialog Example ==
== QProgressDialog Example ==
Line 105: Line 190:
=== form.h ===
=== form.h ===


<code><br />#ifndef FORM_H<br />#define FORM_H
<code>
#ifndef FORM_H
#define FORM_H


#include &quot;ui_form.h&amp;quot;
#include "ui_form.h"


#include &lt;QFutureWatcher&amp;gt;
#include <QFutureWatcher>


#include &quot;MyClass.h&amp;quot;
#include "MyClass.h"


class QProgressDialog;
class QProgressDialog;


class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
class Form : public QWidget, private Ui::Form
{
Q_OBJECT


public slots:
public slots:


void slot_finished();<br /> void on_pushButton_clicked();
void slot_finished();
void on_pushButton_clicked();
 
public:
Form(QWidget '''parent = 0);


public:<br /> Form(QWidget '''parent = 0);
private:
<br />private:<br /> QFutureWatcher&amp;lt;void&amp;gt; FutureWatcher;<br /> MyClass MyObject;<br /> QProgressDialog''' ProgressDialog;<br />};
QFutureWatcher<void> FutureWatcher;
MyClass MyObject;
QProgressDialog''' ProgressDialog;
};


#endif<br /></code>
#endif
</code>


=== form.cpp ===
=== form.cpp ===


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


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


#include &lt;iostream&amp;gt;
#include <iostream>


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


this-&gt;ProgressDialog = new QProgressDialog(this);
this->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()));
connect(&this->FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));
connect(&this->FutureWatcher, SIGNAL (finished()), this->ProgressDialog , SLOT (cancel()));


}
}


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


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);
void Form::on_pushButton_clicked()
{
// Start the computation.
QFuture<void> future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);
this->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;;
this->ProgressDialog->setMinimum(0);
this->ProgressDialog->setMaximum(0);
this->ProgressDialog->setWindowModality(Qt::WindowModal);
this->ProgressDialog->exec();


}<br /></code>
}
</code>


=== form.ui ===
=== form.ui ===


<code><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 /></code>
<code>
<?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>
</code>


MyClass.h<br /><code><br />#ifndef MyClass_H<br />#define MyClass_H
MyClass.h
<code>
#ifndef MyClass_H
#define MyClass_H


#include &lt;iostream&amp;gt;
#include <iostream>


class MyClass<br />{<br />public:
class MyClass
{
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 />};
void LongFunction()
{
for( int count = 0; count < 5; count++ )
{
sleep( 1 );
std::cout << "Ping long!" << std::endl;
}
}
};


#endif<br /></code>
#endif
</code>


=== FutureWatcherProgressDialog.cpp ===
=== FutureWatcherProgressDialog.cpp ===


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


#include &lt;iostream&amp;gt;
#include <iostream>


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


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


Form form;
Form form;
Line 178: Line 339:
form.show();
form.show();


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

Latest revision as of 22:25, 27 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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 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 and QtConcurrent::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
#define FORM_H

#include "ui_form.h"

#include <QFutureWatcher>

#include "MyClass.h"

class Form : public QWidget, private Ui::Form
{
Q_OBJECT

public slots:

void slot_finished();
 void on_pushButton_clicked();

public:
 Form(QWidget *parent = 0);

private:
 QFutureWatcher<void> FutureWatcher;
 MyClass MyObject;
};

#endif

form.cpp

#include <QtGui>
#include <QImage>

#include "form.h"

#include <iostream>

Form::Form(QWidget *parent)
 : QWidget(parent)
{
 setupUi(this);

this->progressBar->setMinimum(0);
 this->progressBar->setMaximum(0);
 this->progressBar->hide();

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

}

void Form::slot_finished()
{
 this->progressBar->hide();
}

void Form::on_pushButton_clicked()
{
 this->progressBar->show();

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

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>
#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();
}

MyClass.h

#ifndef MyClass_H
#define MyClass_H

#include <iostream>

class MyClass
{
public:

void LongFunction()
 {
 for( int count = 0; count < 5; count++ )
 {
 sleep( 1 );
 std::cout << "Ping long!" << std::endl;
 }
 }
};

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT (FutureWatcher)

FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE (${QT_USE_FILE})

QT4_WRAP_CPP(MOCSrcs form.h)
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})
TARGET_LINK_LIBRARIES(FutureWatcher ${QT_LIBRARIES})

QProgressDialog Example

form.h

#ifndef FORM_H
#define FORM_H

#include "ui_form.h"

#include <QFutureWatcher>

#include "MyClass.h"

class QProgressDialog;

class Form : public QWidget, private Ui::Form
{
Q_OBJECT

public slots:

void slot_finished();
 void on_pushButton_clicked();

public:
 Form(QWidget '''parent = 0);

private:
 QFutureWatcher<void> FutureWatcher;
 MyClass MyObject;
 QProgressDialog''' ProgressDialog;
};

#endif

form.cpp

#include <QtGui>
#include <QImage>

#include "form.h"

#include <iostream>

Form::Form(QWidget *parent)
 : QWidget(parent)
{
 setupUi(this);

this->ProgressDialog = new QProgressDialog(this);

connect(&this->FutureWatcher, SIGNAL (finished()), this, SLOT (slot_finished()));
 connect(&this->FutureWatcher, SIGNAL (finished()), this->ProgressDialog , SLOT (cancel()));

}

void Form::slot_finished()
{
 std::cout << "Finshed" << std::endl;
}

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

this->ProgressDialog->setMinimum(0);
 this->ProgressDialog->setMaximum(0);
 this->ProgressDialog->setWindowModality(Qt::WindowModal);
 this->ProgressDialog->exec();

}

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

#ifndef MyClass_H
#define MyClass_H

#include <iostream>

class MyClass
{
public:

void LongFunction()
 {
 for( int count = 0; count < 5; count++ )
 {
 sleep( 1 );
 std::cout << "Ping long!" << std::endl;
 }
 }
};

#endif

FutureWatcherProgressDialog.cpp

#include <QApplication>

  1. include <QObject>
  2. include <QThread>
  1. include <iostream>
  1. include "form.h"

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

QApplication app(argc, argv);

Form form;

form.show();

return app.exec();