QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11TARGET = app_in_threadTEMPLATE = appSOURCES += main.cpp
#include <QApplication>#include <QThread>#include <QPushButton>class QxAppShell: public QThread{public: QxAppShell() { QThread::start(); } void run() Q_DECL_OVERRIDE { int argc=0; char *argv=0; QApplication app(argc, &argv); QPushButton button; button.setText("Hello, World"); button.show(); app.exec(); }} *g_app;int main(int argc, char *argv[]){ g_app = new QxAppShell; while(true) QThread::msleep(1); return 0;}
C++ (Qt)#include <QtWidgets>#include <QThread>#include <qwindowsysteminterface.h> class QxAppShell: public QThread{public: QxAppShell() { moveToThread(this); QThread::start(); } void run() Q_DECL_OVERRIDE { int argc = 0; char * argv = 0; QApplication app(argc, &argv); QWindowSystemInterface::setSynchronousWindowsSystemEvents(true); QPushButton button; button.setText("Hello, World"); button.show(); app.exec(); } } *g_app; int main(int argc, char *argv[]){ g_app = new QxAppShell; while (true) QThread::msleep(1); return 0;}