Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

QtConcurrent-run-member-function: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Fix code blocks formatting)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
[[Category:HowTo]]


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]]).
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 ==
==form.h==
<syntaxhighlight lang="c++">


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


#include &quot;ui_form.h&amp;quot;
#include "ui_form.h"
 
#include "MyClass.h"
#include &quot;MyClass.h&amp;quot;
 
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 on_pushButton_clicked();


void on_pushButton_clicked();
public:
 
Form(QWidget *parent = 0);
public:<br /> Form(QWidget *parent = 0);
 
private:<br /> MyClass MyObject;


private:
MyClass MyObject;
};
};


#endif</code>
#endif
</syntaxhighlight>


== form.cpp ==
==form.cpp==
<syntaxhighlight lang="c++">
#include <QtGui>
#include <QImage>


<code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QImage&amp;gt;
#include "form.h"


#include &quot;form.h&amp;quot;
#include <iostream>


#include &lt;iostream&amp;gt;
Form::Form(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
}


Form::Form(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
void Form::on_pushButton_clicked()
{
// Start the computation.
QFuture<void> future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);


}
}
</syntaxhighlight>


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);
==form.ui==


}<br /></code>
<syntaxhighlight lang="xml">
<?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/>


== form.ui ==
</ui>
</syntaxhighlight>


<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>
==MyClass.h==
<syntaxhighlight lang="c++">
#ifndef MyClass_H
#define MyClass_H


== MyClass.h ==
#include <iostream>


<code><br />#ifndef MyClass_H<br />#define MyClass_H
class MyClass
{
public:
void LongFunction() {
  for( int count = 0; count < 5; count++ ) {
  sleep( 1 );
  std::cout << "Ping long!" << std::endl;
  }
}
};


#include &lt;iostream&amp;gt;
#endif
</syntaxhighlight>


class MyClass<br />{<br />public:
==main.cpp==


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 />};
<syntaxhighlight lang="c++">
#include <QApplication>
#include <QObject>
#include <QThread>
#include <iostream>


#endif<br /></code>
#include "form.h"


== main.cpp ==
int main(int argc, char*argv[]) {
QApplication app(argc, argv);
Form form;
form.show();
return app.exec();


<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QObject&amp;gt;<br />#include &lt;QThread&amp;gt;
}
 
</syntaxhighlight>
#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;;

Latest revision as of 21:51, 12 April 2023


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
#define FORM_H

#include "ui_form.h"
#include "MyClass.h"

class Form : public QWidget, private Ui::Form
{
 Q_OBJECT
public slots:
 void on_pushButton_clicked();

public:
 Form(QWidget *parent = 0);

private:
 MyClass MyObject;
};

#endif

form.cpp

#include <QtGui>
#include <QImage>

#include "form.h"

#include <iostream>

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

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

}

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

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

}