Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ Registered Type QInvokable Call

C++ Registered Type QInvokable Call

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tobias.h
    wrote on last edited by
    #1

    Hello,
    I have built a little QML-Application and have a problem: When i call a C++ Function, exposed by QINVOKABLE macro, the GUI freeze until the call returns.

    Details:
    I build a c++ class database that handles a database connection. The class provides functions like connect or loadDataInRam. These functions are exposed by the Q_INVOKABLE.

    The class database is registered for usage in QML:
    @qmlRegisterType<Database>("com.mycompany.qmlcomponents", 0,5, "Database");@

    The new type "Database" is instantiated in QML.
    @ Database {
    id: database
    }@

    Now i call the functions e.g. in a button slot

    @Button{
    id:btconnect
    text: "Connect"
    onClicked: {
    database.connect()
    ....
    database.loadDataInRam()
    }
    }@

    The GUI freeze until the onClicked block is performed.... I tried to use the worker script for getting a new "buisnesslogic performer thread" but it turns out that this generates a new JS-Engine without access to the QML components.
    Another option is to trigger threads in the database class, so all functions just start threads and return immediately, but i want to avoid that implementation overhead.

    The best way I read is:
    Move the QObject derivate class database to another (new) thread with moveToThread ( QThread * targetThread ). Change the Q_INVOKABLE to exposed slots. And trigger the slots from QML.

    Is it right that the slot calls are stored in a queue and are processed by the registered thread.
    Is possible to return something? Where i can find the signal slot threading behavior documentaion?

    Is this the way to go? I am looking for a kind of "best practice".

    Thanks
    Tobi

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.h
      wrote on last edited by
      #2

      No one?

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi, and welcome to the Qt Dev Net!

        [quote]The best way I read is:
        Move the QObject derivate class database to another (new) thread with moveToThread ( QThread * targetThread ). Change the Q_INVOKABLE to exposed slots. And trigger the slots from QML.[/quote]Yes, I would recommend this approach too.

        [quote]Is it right that the slot calls are stored in a queue and are processed by the registered thread.[/quote]Correct.

        [quote]Is possible to return something?[/quote]You can emit a C++ signal, and connect it to a QML slot/function. Or, you can invoke the QML function using a queued connection. See "Interacting with QML Objects from C++|Invoking QML Methods":http://qt-project.org/doc/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

        [quote]Where i can find the signal slot threading behavior documentaion?[/quote]The most important concepts are at "QObject|Thread Affinity":http://qt-project.org/doc/qt-5/QObject.html#thread-affinity

        The "QThread documentation":http://qt-project.org/doc/qt-5/qthread.html has good information too.

        Finally, see "Multithreading Technologies in Qt":http://qt-project.org/doc/qt-5/threads-technologies.html for a general overview.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved