Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit:</strong> rework of entire post to cover basics as well.</p> <p><code>Background.h</code>:</p> <pre><code>#ifndef BACKGROUND_H #define BACKGROUND_H #include &lt;QThread&gt; #include &lt;QObject&gt; class Background : public QThread { Q_OBJECT public: Background(QObject* parent = 0):QThread(parent){} protected: void run() { qDebug(qPrintable(QString("Child function ran in thread: %1").arg(QThread::currentThreadId()))); } }; class BackgroundConcurrent : public QObject { Q_OBJECT public: BackgroundConcurrent(QObject* parent = 0):QObject(parent){} public slots: void doWork() const { qDebug(qPrintable(QString("Concurrent child function ran in thread: %1").arg(QThread::currentThreadId()))); } }; class BackgroundTrigger : public QObject { Q_OBJECT public: BackgroundTrigger(QObject* parent = 0):QObject(parent){} ~BackgroundTrigger() { foreach(QObject* child, children()) { QThread* childThread = qobject_cast&lt;QThread*&gt;(child); if (childThread) childThread-&gt;wait(); } } public slots: void triggerWorker() { Background* child = new Background(this); child-&gt;start(); } }; #endif // BACKGROUND_H </code></pre> <p><code>main.cpp</code>:</p> <pre><code>#include "Background.h" #include &lt;QCoreApplication&gt; #include &lt;QtConcurrentRun&gt; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Using QThread BackgroundTrigger childTrigger; qDebug(qPrintable(QString("Main function ran in thread: %1").arg(QThread::currentThreadId()))); // Call child childTrigger.triggerWorker(); childTrigger.triggerWorker(); // Using QtConcurrent BackgroundConcurrent cchild; QFuture&lt;void&gt; future1 = QtConcurrent::run(&amp;cchild, &amp;BackgroundConcurrent::doWork); QFuture&lt;void&gt; future2 = QtConcurrent::run(&amp;cchild, &amp;BackgroundConcurrent::doWork); return 0; } </code></pre> <p>Sample output:</p> <pre>Main function ran in thread: 1087038064 Child function ran in thread: 1091267472 Child function ran in thread: 1093417872 Concurrent child function ran in thread: 1095519120 Concurrent child function ran in thread: 1097644944</pre> <p>Be sure you run moc on your header files, <a href="http://doc.trolltech.com/4.5/qmake-manual.html" rel="nofollow noreferrer">qmake</a> and <a href="http://www.cmake.org/cmake/help/documentation.html" rel="nofollow noreferrer">cmake</a> both support creating your makefiles.</p> <p>Here is the <code>CMakeLists.txt</code> file I used to build the code:</p> <pre><code>cmake_minimum_required(VERSION 2.6) #Project name project(TEST) #Use Qt4 find_package(Qt4) if(QT4_FOUND) set(QT_USE_QTOPENGL TRUE) include(${QT_USE_FILE}) set(LIBS ${QT_LIBRARIES} ) #Source files (*.cpp, *.o) set(TEST_SRCS main.cpp) #Header files (*.h[pp]) set(TEST_HDRS Background.h) #Qt macros to handle uic, moc, etc... QT4_WRAP_CPP(TEST_MOC ${TEST_HDRS} OPTIONS -nw) set(TEST_ALLSRC ${TEST_SRCS} ${TEST_MOC}) #Create main add_executable(test ${TEST_ALLSRC}) target_link_libraries(test ${LIBS}) endif(QT4_FOUND) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload