QtConcurrent-run-member-function: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(→‎form.ui: code lang=xml)
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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|QtConcurrent run inherited member function]]).
[[Category:HowTo]]


==form.h==
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.cpp==
== form.h ==


==form.ui==
<code>
#ifndef FORM_H
#define FORM_H


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


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


===Categories:===
public:
Form(QWidget *parent = 0);


* [[:Category:snippets|snippets]]
private:
MyClass MyObject;
};
 
#endif
</code>
 
== form.cpp ==
 
<code>
#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);
}
</code>
 
== form.ui ==
 
<code 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/>
</ui>
</code>
 
== MyClass.h ==
 
<code>
#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
</code>
 
== main.cpp ==
 
<code>
#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();
}
</code>

Revision as of 02:24, 18 May 2017


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