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
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Remove non-functioning "toc" command) |
(Fix code blocks formatting) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:HowTo]] | |||
[[Category: | |||
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++"> | |||
#ifndef FORM_H | |||
#define FORM_H | #define FORM_H | ||
#include "ui_form.h" | #include "ui_form.h" | ||
#include "MyClass.h" | #include "MyClass.h" | ||
class Form : public QWidget, private Ui::Form | class Form : public QWidget, private Ui::Form | ||
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
public slots: | public slots: | ||
void on_pushButton_clicked(); | |||
void on_pushButton_clicked(); | |||
public: | public: | ||
Line 27: | Line 23: | ||
private: | private: | ||
MyClass MyObject; | MyClass MyObject; | ||
}; | }; | ||
#endif</ | #endif | ||
</syntaxhighlight> | |||
< | ==form.cpp== | ||
<syntaxhighlight lang="c++"> | |||
#include <QtGui> | #include <QtGui> | ||
#include <QImage> | #include <QImage> | ||
Line 46: | Line 41: | ||
{ | { | ||
setupUi(this); | setupUi(this); | ||
} | } | ||
Line 55: | Line 49: | ||
} | } | ||
</ | </syntaxhighlight> | ||
== form.ui == | ==form.ui== | ||
< | <syntaxhighlight lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | <ui version="4.0"> | ||
Line 87: | Line 81: | ||
<resources/> | <resources/> | ||
<connections/> | <connections/> | ||
</ui> | </ui> | ||
</ | </syntaxhighlight> | ||
== MyClass.h == | ==MyClass.h== | ||
<syntaxhighlight lang="c++"> | |||
< | |||
#ifndef MyClass_H | #ifndef MyClass_H | ||
#define MyClass_H | #define MyClass_H | ||
Line 101: | Line 95: | ||
{ | { | ||
public: | public: | ||
void LongFunction() { | |||
void LongFunction() | for( int count = 0; count < 5; count++ ) { | ||
sleep( 1 ); | |||
std::cout << "Ping long!" << std::endl; | |||
} | |||
} | } | ||
}; | }; | ||
#endif | #endif | ||
</ | </syntaxhighlight> | ||
== main.cpp == | ==main.cpp== | ||
< | <syntaxhighlight lang="c++"> | ||
#include <QApplication> | #include <QApplication> | ||
#include <QObject> | #include <QObject> | ||
#include <QThread> | #include <QThread> | ||
#include <iostream> | #include <iostream> | ||
#include "form.h" | #include "form.h" | ||
int main(int argc, char*argv[]) | int main(int argc, char*argv[]) { | ||
{ | |||
QApplication app(argc, argv); | QApplication app(argc, argv); | ||
Form form; | |||
form.show(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | |||
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();
}