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-inherited-member-function: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[toc align_right= | [toc align_right="yes" depth="3"] | ||
To call an inherited member function of an object using QtConcurrent::run, you must cast the member function address to the subclass type. | To call an inherited member function of an object using QtConcurrent::run, you must cast the member function address to the subclass type. | ||
Line 7: | Line 7: | ||
== form.h == | == form.h == | ||
<code> | <code> | ||
#ifndef FORM_H | |||
#define FORM_H | |||
#include | #include "ui_form.h" | ||
#include | #include "MyClass.h" | ||
class Form : public QWidget, private Ui::Form | class Form : public QWidget, private Ui::Form | ||
{ | |||
Q_OBJECT | |||
public slots: | public slots: | ||
void slot_finished(); | void slot_finished(); | ||
void on_pushButton_clicked(); | |||
public: | public: | ||
Form(QWidget *parent = 0); | |||
private: | private: | ||
ChildClass MyObject; | |||
}; | }; | ||
Line 31: | Line 38: | ||
== form.cpp == | == form.cpp == | ||
<code> | <code> | ||
#include <QtGui> | |||
#include <QImage> | |||
#include | #include "form.h" | ||
#include "MyClass.h" | |||
#include | #include <iostream> | ||
Form::Form(QWidget '''parent) | Form::Form(QWidget '''parent) | ||
: QWidget(parent) | |||
{ | |||
setupUi(this); | |||
} | |||
void Form::on_pushButton_clicked() | |||
{ | |||
QFuture<void> future = QtConcurrent::run(&this->MyObject, static_cast<void(ChildClass::''')()>(&ChildClass::LongFunction));// works | |||
} | |||
</code> | </code> | ||
Line 45: | Line 63: | ||
== form.ui == | == form.ui == | ||
<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> | </code> | ||
Line 51: | Line 97: | ||
== MyClass.h == | == MyClass.h == | ||
<code> | <code> | ||
#ifndef MyClass_H | |||
#define MyClass_H | |||
#include | #include <iostream> | ||
class ParentClass | class ParentClass | ||
{ | |||
public: | |||
virtual void LongFunction() | virtual void LongFunction() | ||
{ | |||
for( int count = 0; count < 5; count++ ) | |||
{ | |||
sleep( 1 ); | |||
std::cout << "Ping long!" << std::endl; | |||
} | |||
} | |||
}; | |||
class ChildClass : public ParentClass | class ChildClass : public ParentClass | ||
{ | |||
public: | |||
void LongFunction() | void LongFunction() | ||
{ | |||
for( int count = 0; count < 5; count++ ) | |||
{ | |||
sleep( 1 ); | |||
std::cout << "Ping long!" << std::endl; | |||
} | |||
} | |||
}; | |||
#endif | #endif | ||
Line 69: | Line 137: | ||
== main.cpp == | == main.cpp == | ||
<code> | <code> | ||
#include <QApplication> | |||
#include <QObject> | |||
#include <QThread> | |||
#include | #include <iostream> | ||
#include | #include "form.h" | ||
int main(int argc, char*argv[]) | int main(int argc, char*argv[]) | ||
{ | |||
QApplication app(argc, argv); | |||
Form form; | Form form; | ||
Line 81: | Line 154: | ||
form.show(); | form.show(); | ||
return app.exec | return app.exec(); | ||
} |
Revision as of 09:55, 25 February 2015
[toc align_right="yes" depth="3"]
To call an inherited member function of an object using QtConcurrent::run, you must cast the member function address to the subclass type.
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 slot_finished();
void on_pushButton_clicked();
public:
Form(QWidget *parent = 0);
private:
ChildClass MyObject;
};
#endif
form.cpp
#include <QtGui>
#include <QImage>
#include "form.h"
#include "MyClass.h"
#include <iostream>
Form::Form(QWidget '''parent)
: QWidget(parent)
{
setupUi(this);
}
void Form::on_pushButton_clicked()
{
QFuture<void> future = QtConcurrent::run(&this->MyObject, static_cast<void(ChildClass::''')()>(&ChildClass::LongFunction));// works
}
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 ParentClass
{
public:
virtual void LongFunction()
{
for( int count = 0; count < 5; count++ )
{
sleep( 1 );
std::cout << "Ping long!" << std::endl;
}
}
};
class ChildClass : public ParentClass
{
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();
}