简体   繁体   中英

How to create QtQuick window outside the main thread

What I'd like to have is a main thread that instantiates a class that extends QQuickView and moves it to a second thread.

Ideally, I would like to do something like this:

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    MyClass myClassObj;

    myClassObj.init();

    return 0;
}

MyClass.cpp

void init()
{
    MyQtQuickClass view;

    QThread* GUIthread = new QThread(this);

    view.moveToThread(GUIthread);
    QObject::connect(GUIthread, SIGNAL(started()), &view, SLOT(init()));

    GUIthread.start();
}

MyQtQuickCLass.cpp

void init()
{
    QQmlContext* rootContext = this->rootContext();

    // Setup view code here

    this->show();

    QGuiApplication::instance()->exec();
}

With something like this I get this error: QQmlEngine: Illegal attempt to connect to QQmlContext(0x120fc60) that is in a different thread than the QML engine QQmlEngine(0xdf6e70).

Is there a workaround? Or a way to create the QML engine directly in the second thread?

QObject s have thread affinity to the parent QObject 's thread, or the current thread if they don't have a parent. Most likely -- in your example -- some QObject s created during instantiation of MyQtQuickClass have affinity to the thread in which they were created. Creating the MyQtQuickClass in the GUIthread would solve this.

However , all Qt windows must run in the same thread as QGuiApplication , and your application would most likely crash if you tried.

If you want a QQuickView to live outside the main() thread, then you must:

  1. Create a new std::thread (NOT a QThread , because it is a QObject so it mustn't be created before your QGuiApplication )
  2. Run an init() function in that thread. Let it instantiate your QGuiApplication and start the event loop.
  3. Create your QQuickView in that thread too.
  4. Ensure that all of your GUI objects are accessed from that thread only.
  5. Ensure that your main() thread doesn't create any QObject s until after your QGuiApplication has been created in your other thread.

See How to avoid Qt app.exec() blocking main thread for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM