QtConcurrent-run-member-function: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[toc align_right="yes&quot; depth="3&quot;] | |||
You can call a member function of an object using QtConcurrent::run as follows. (Note for an inherited member function, please see [[QtConcurrent-run-inherited-member-function]]). | |||
==form. | == form.h == | ||
<code>#ifndef FORM_H<br />#define FORM_H | |||
#include "ui_form.h&quot; | |||
#include "MyClass.h&quot; | |||
class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT | |||
* | public slots: | ||
void on_pushButton_clicked(); | |||
public:<br /> Form(QWidget *parent = 0); | |||
private:<br /> MyClass MyObject; | |||
}; | |||
#endif</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); | |||
} | |||
void Form::on_pushButton_clicked()<br />{<br /> // Start the computation.<br /> QFuture&lt;void&gt; future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction); | |||
}<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 == | |||
<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> | |||
== main.cpp == | |||
<code><br />#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:41, 23 February 2015
[toc align_right="yes" depth="3"]
You can call a member function of an object using QtConcurrent::run as follows. (Note for an inherited member function, please see QtConcurrent-run-inherited-member-function).
form.h
#ifndef FORM_H<br />#define FORM_H
#include "ui_form.h&quot;
#include "MyClass.h&quot;
class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
public slots:
void on_pushButton_clicked();
public:<br /> Form(QWidget *parent = 0);
private:<br /> MyClass MyObject;
};
#endif
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);
}
void Form::on_pushButton_clicked()<br />{<br /> // Start the computation.<br /> QFuture&lt;void&gt; future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);
}<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 />
main.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;);