Qt World Summit: Submit your Presentation

Bind QObject created in different thread to QML interface ran in main thread

  • Hi guys,

    I'm writing QtQuick application, I defined C++ class, with Q_PROPERTIES, then registered class, and now able to call its method and properties from QML code.

    But I ran into troubles. I have asynchronies communication with a server. In main thread I'm calling 'getServerData' function, which is non-blocking, I also has an event triggered from different thread onGetServerData()

    When I got data from server, I'm creating bunch of QCustomFile objects, and updating interface. ( Binding their properties to QML ). And here is a problem, since QCustomFile objects were created in different thread, runtime error is raised by Qt Framework:

    @QQmlEngine: Illegal attempt to connect to QCustomFile(0x104299780) that is in a different thread than the QML engine QQmlApplicationEngine(0x7fff5fbff5b8)@

    Does anyone know, how to update QML interface from different thread?

  • First your QCustomFile must be a QObject child. Create and load your QCustomFile on your thread, then use QObject::moveToThread(<GET YOU MAIN THREAD HERE>). Then you send it through a signal to your main thread... Hope it works for you..

Log in to reply