creating QApplication in a different thread

2020-03-30 02:34发布

I'm trying to create QApplication in a different thread, but found 2 main problems:
1- I can't interact with GUI
2- some warnings:
WARNING: QApplication was not created in the main() thread. QObject::startTimer: timers cannot be started from another thread //happens when resizing widget QObject::killTimer: timers cannot be stopped from another thread

here is the full code: (it may has some memory leaks but for testing purposes it fails)

//main.cpp

#include <QCoreApplication>
#include "cthread.h"

int main(int argc, char *argv[])
{
    CThread *MyThread = new CThread;
    MyThread->start();

    QCoreApplication a(argc, argv);

    return a.exec();
}

//CThread.h

#ifndef CTHREAD_H
#define CTHREAD_H

#include <QThread>
#include "theqtworld.h"

class CThread : public QThread
{
    Q_OBJECT
public:
    CThread();
    void run( void );

private:
    TheQtWorld *mWorld;
};

#endif // CTHREAD_H

//CThread.cpp

#include "cthread.h"
#include <iostream>

CThread::CThread():mWorld(NULL)
{
}


void CThread::run()
{
    std::cout << "thread started" << std::endl;
    if(!mWorld)
        mWorld = new TheQtWorld();

    mWorld->OpenWorld();//now it will init all Qt Stuff inside

//    if(mWorld) delete mWorld;
//    emit this->exit();
}

//theqtworld.h

#ifndef THEQTWORLD_H
#define THEQTWORLD_H

#include <QObject>
#include "mainwindow.h"
#include <QApplication>

class TheQtWorld : public QObject
{
    Q_OBJECT
public:
    explicit TheQtWorld(QObject *parent = 0);
    int OpenWorld(void);

signals:

public slots:

};

#endif // THEQTWORLD_H

//theqtworld.cpp

#include "theqtworld.h"

TheQtWorld::TheQtWorld(QObject *parent) :
    QObject(parent)
{
}

int TheQtWorld::OpenWorld()
{
    static int arg = 0;
    static char *b[2];
    b[0] = "a";

    QApplication *a = new QApplication(arg, b);
    a->setParent(this);

    MainWindow w;
    w.show();

    return a->exec();
}

1条回答
做个烂人
2楼-- · 2020-03-30 03:13

I would answer my own question after understanding how to overcome this problem

first the problem was to integrate Qt GUI as a plugin into another Application, so the main issue was the Event loop collision between Qt Events and any other Application Events

my first thoughts was to separate both, so QApplication will stay at a different thread, but this was a totally wrong approach and here is what I have noticed:

1- Qt GUI Must stay in the main() thread so there is no other place for QApplication
2- to avoid the blocking QApplication::exec() , embed QApplication::processEvents() into the other Application Event loop

here is a working code:

//main.cpp

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication mApp(argc, argv);
    MainWindow w;
    w.show();

    //just for testing and holding the program so it doesn't end
    for(int i = 0; i < 100000000; ++i)
    {
        mApp.processEvents();
    }
    return 0;
}

edit:thanks to pavel-strakhov for his great suggestion.

查看更多
登录 后发表回答