#ifndef TC_H#define TC_H#include <QThread>#include <QDebug>class ThreadClass : public QThread{public: ThreadClass(QObject *parent = 0) : QThread(parent) { } void execMethod() { qDebug() << "ThreadClass->execMethod()-> ID = " << QThread::currentThreadId(); }protected: void run() { qDebug() << "ThreadClass->run()-> ID = " << QThread::currentThreadId(); exec(); qDebug() << "After Exec"; }};#endif
#include <QtCore>#include <tc.h>int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); qDebug() << "QCoreApplication -> ID = " << QThread::currentThreadId(); ThreadClass *tc = new ThreadClass(); tc->moveToThread(tc); tc->start(); sleep(1); tc->execMethod(); return app.exec();}
#ifndef TC_H#define TC_H#include <QThread>#include <QDebug>class PrintClass : public QObject{ Q_OBJECTpublic: PrintClass() {} virtual ~PrintClass() {}public slots: void printMsg() { qDebug() << "PrintClass->printMsg()-> ID = " << QThread::currentThreadId(); }};class ThreadClass : public QThread{ Q_OBJECTpublic: ThreadClass() {} virtual ~ThreadClass() {} void execMethod() { qDebug() << "ThreadClass->execMethod()-> ID = " << QThread::currentThreadId(); emit sigExec(); }protected: void run() { qDebug() << "ThreadClass->run()-> ID = " << QThread::currentThreadId(); pc = new PrintClass(); connect(this, SIGNAL(sigExec()), pc, SLOT(printMsg())); exec(); qDebug() << "After Exec"; }signals: void sigExec();private: PrintClass *pc;};#endif
#include <QtCore>#include "tc.h"int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); qDebug() << "QCoreApplication -> ID = " << QThread::currentThreadId(); ThreadClass *tc = new ThreadClass(); tc->start(); sleep(1); tc->execMethod(); return app.exec();}
#ifndef TC_H#define TC_H#include <QThread>#include <QDebug>class ThreadClass : public QThread{ Q_OBJECTpublic: ThreadClass() {} virtual ~ThreadClass() {} void execMethod() { qDebug() << "ThreadClass->execMethod()-> ID = " << QThread::currentThreadId(); emit sigExec(); }protected: void run() { qDebug() << "ThreadClass->run()-> ID = " << QThread::currentThreadId(); connect(this, SIGNAL(sigExec()), this, SLOT(printMsg())); exec(); qDebug() << "After Exec"; }signals: void sigExec();private slots: void printMsg() { qDebug() << "ThreadClass->printMsg()-> ID = " << QThread::currentThreadId(); }};#endif
#include <QtCore>#include <QtTest/QtTest>#include "tc.h"int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); qDebug() << "QCoreApplication -> ID = " << QThread::currentThreadId(); ThreadClass *tc = new ThreadClass(); tc->moveToThread(tc); // <---- ВОТ БЕЗ ЭТОГО НЕ ПРАВИЛЬНО РАБОТАЕТ tc->start(); QTest::qSleep(250); tc->execMethod(); return app.exec();}
void execMethod() {}QFuture<void> QtConcurrent::run (execMethod)