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-free-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]]
[[Category:snippets]]


[toc align_right="yes" depth="3"]
[toc align_right="yes" depth="3"]


To call a free function using QtConcurrent::run, simply pass the function name.
To call a free function using QtConcurrent::run, simply pass the function name.
Line 7: Line 7:
== form.h ==
== form.h ==


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


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


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


public slots:
public slots:
Line 17: Line 21:
void on_btnOpen_clicked();
void on_btnOpen_clicked();


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


};
};
Line 27: Line 32:
== form.cpp ==
== form.cpp ==


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


#include &quot;form.h&amp;quot;
#include "form.h"


#include &lt;iostream&amp;gt;
#include <iostream>


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


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 />}
void LongFunction()
{
for( int count = 0; count < 5; count++ )
{
sleep( 1 );
std::cout << "Ping long!" << std::endl;
}
}


void Form::on_btnOpen_clicked()<br />{<br /> QFuture&amp;lt;void&amp;gt; result = QtConcurrent::run ( LongFunction );<br /> //result.waitForFinished();
void Form::on_btnOpen_clicked()
{
QFuture<void> result = QtConcurrent::run ( LongFunction );
//result.waitForFinished();


std::cout &lt;&lt; &quot;exit.&quot; &lt;&lt; std::endl;<br />}
std::cout << "exit." << std::endl;
}


</code>
</code>
Line 45: Line 67:
== form.ui ==
== 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>
<?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 101:
== main.cpp ==
== main.cpp ==


<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QObject&amp;gt;<br />#include &lt;QThread&amp;gt;
<code>
#include <QApplication>
#include <QObject>
#include <QThread>


#include &lt;iostream&amp;gt;
#include <iostream>


#include &quot;form.h&amp;quot;
#include "form.h"


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


Form form;
Form form;
Line 63: Line 118:
form.show();
form.show();


return app.exec&amp;amp;#40;&amp;#41;;<br />}
return app.exec();
}

Revision as of 09:48, 25 February 2015


[toc align_right="yes" depth="3"]

To call a free function using QtConcurrent::run, simply pass the function name.

form.h

#ifndef FORM_H
#define FORM_H

#include "ui_form.h"

class Form : public QWidget, private Ui::Form
{
Q_OBJECT

public slots:

void on_btnOpen_clicked();

public:
 Form(QWidget *parent = 0);

};

#endif

form.cpp

#include <QtGui>
#include <QImage>

#include "form.h"

#include <iostream>

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

void LongFunction()
{
 for( int count = 0; count < 5; count++ )
 {
 sleep( 1 );
 std::cout << "Ping long!" << std::endl;
 }
}

void Form::on_btnOpen_clicked()
{
 QFuture<void> result = QtConcurrent::run ( LongFunction );
 //result.waitForFinished();

std::cout << "exit." << std::endl;
}

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>

main.cpp

  1. include <QApplication>
  2. include <QObject>
  3. include <QThread>
  1. include <iostream>
  1. include "form.h"

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

QApplication app(argc, argv);

Form form;

form.show();

return app.exec(); }