QtConcurrent-run-free-function: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (→form.ui: code lang=xml) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
[[Category: | [[Category:HowTo]] | ||
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. | ||
== form.h == | == form.h == | ||
<code> | <code> | ||
#ifndef FORM_H | |||
#define FORM_H | |||
#include | #include "ui_form.h" | ||
class Form : public QWidget, private Ui::Form | class Form : public QWidget, private Ui::Form | ||
{ | |||
Q_OBJECT | |||
public slots: | public slots: | ||
Line 17: | Line 18: | ||
void on_btnOpen_clicked(); | void on_btnOpen_clicked(); | ||
public: | public: | ||
Form(QWidget *parent = 0); | |||
}; | }; | ||
#endif | #endif | ||
</code> | </code> | ||
== form.cpp == | == form.cpp == | ||
<code> | <code> | ||
#include <QtGui> | |||
#include <QImage> | |||
#include | #include "form.h" | ||
#include | #include <iostream> | ||
Form::Form(QWidget *parent) | Form::Form(QWidget *parent) | ||
: QWidget(parent) | |||
{ | |||
setupUi(this); | |||
} | |||
void LongFunction() | void LongFunction() | ||
{ | |||
for( int count = 0; count < 5; count++ ) | |||
{ | |||
sleep( 1 ); | |||
std::cout << "Ping long!" << std::endl; | |||
} | |||
} | |||
void Form::on_btnOpen_clicked() | void Form::on_btnOpen_clicked() | ||
{ | |||
QFuture<void> result = QtConcurrent::run ( LongFunction ); | |||
//result.waitForFinished(); | |||
std::cout << "exit." << std::endl; | |||
} | |||
</code> | </code> | ||
== form.ui == | == form.ui == | ||
<code>< | <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> | </code> | ||
== main.cpp == | == main.cpp == | ||
<code>< | <code> | ||
#include <QApplication> | |||
#include <QObject> | |||
#include <QThread> | |||
#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; | form.show(); | ||
return app.exec(); | |||
form.show(); | } | ||
</code> | |||
return app.exec |
Latest revision as of 02:18, 18 May 2017
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
#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();
}