QtConcurrent-run-inherited-member-function: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[Category:snippets]]
[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.


==form.h==
== form.h ==
 
<code><br />#ifndef FORM_H<br />#define FORM_H
 
#include &quot;ui_form.h&amp;quot;
 
#include &quot;MyClass.h&amp;quot;
 
class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT
 
public slots:
 
void slot_finished();<br /> void on_pushButton_clicked();
 
public:<br /> Form(QWidget *parent = 0);
 
private:<br /> ChildClass MyObject;
 
};
 
#endif
 
</code>
 
== form.cpp ==
 
<code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QImage&amp;gt;
 
#include &quot;form.h&amp;quot;<br />#include &quot;MyClass.h&amp;quot;
 
#include &lt;iostream&amp;gt;
 
Form::Form(QWidget '''parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);<br />}
 
<br />void Form::on_pushButton_clicked()<br />{<br /> QFuture&amp;lt;void&amp;gt; future = QtConcurrent::run(&amp;this-&gt;MyObject, static_cast&amp;lt;void(ChildClass::''')()&gt;(&amp;ChildClass::LongFunction));// works<br />}
 
</code>
 
== form.ui ==
 
<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;
 
</code>
 
== MyClass.h ==
 
<code><br />#ifndef MyClass_H<br />#define MyClass_H
 
#include &lt;iostream&amp;gt;
 
class ParentClass<br />{<br />public:
 
virtual 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 />};
 
class ChildClass : public ParentClass<br />{<br />public:
 
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 />};
 
#endif
 
</code>
 
== main.cpp ==
 
<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QObject&amp;gt;<br />#include &lt;QThread&amp;gt;


==form.cpp==
#include &lt;iostream&amp;gt;


==form.ui==
#include &quot;form.h&amp;quot;


==MyClass.h==
int main(int argc, char*argv[])<br />{<br /> QApplication app(argc, argv);


==main.cpp==
Form form;


===Categories:===
form.show();


* [[:Category:snippets|snippets]]
return app.exec&amp;amp;#40;&amp;#41;;<br />}

Revision as of 09:34, 24 February 2015


[toc align_right="yes&quot; depth="3&quot;]

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

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

#include &quot;ui_form.h&amp;quot;

#include &quot;MyClass.h&amp;quot;

class Form : public QWidget, private Ui::Form<br />{<br />Q_OBJECT

public slots:

void slot_finished();<br /> void on_pushButton_clicked();

public:<br /> Form(QWidget *parent = 0);

private:<br /> ChildClass MyObject;

};

#endif

form.cpp

<br />#include &lt;QtGui&amp;gt;<br />#include &lt;QImage&amp;gt;

#include &quot;form.h&amp;quot;<br />#include &quot;MyClass.h&amp;quot;

#include &lt;iostream&amp;gt;

Form::Form(QWidget '''parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);<br />}

<br />void Form::on_pushButton_clicked()<br />{<br /> QFuture&amp;lt;void&amp;gt; future = QtConcurrent::run(&amp;this-&gt;MyObject, static_cast&amp;lt;void(ChildClass::''')()&gt;(&amp;ChildClass::LongFunction));// works<br />}

form.ui

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

MyClass.h

<br />#ifndef MyClass_H<br />#define MyClass_H

#include &lt;iostream&amp;gt;

class ParentClass<br />{<br />public:

virtual 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 />};

class ChildClass : public ParentClass<br />{<br />public:

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 />};

#endif

main.cpp


#include <QApplication&gt;
#include <QObject&gt;
#include <QThread&gt;

  1. include <iostream&gt;
  1. include "form.h&quot;

int main(int argc, char*argv[])
{
QApplication app(argc, argv);

Form form;

form.show();

return app.exec&amp;#40;&#41;;
}