Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Howto start a member function asynchronously from QML
Forum Updated to NodeBB v4.3 + New Features

Howto start a member function asynchronously from QML

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by
    #1

    In my QML, I'm calling a C++ member function i.e. myClass::recvFromQML() from QML like below

    Action {
    onTriggered:  myClass.recvFromQML(..);
    }
    

    The recvFromQML() takes some time while processing and UI doesn't respond until the function is finished. I want to do that asynchronous so that my GUI doesn't stop.

    Normally one create threads for asynchronous activities, but as I'm calling a member function of myClass ; whats option do I've ? Is their a simpler way of doing this in QT ?

    raven-worxR 1 Reply Last reply
    0
    • P pingal

      In my QML, I'm calling a C++ member function i.e. myClass::recvFromQML() from QML like below

      Action {
      onTriggered:  myClass.recvFromQML(..);
      }
      

      The recvFromQML() takes some time while processing and UI doesn't respond until the function is finished. I want to do that asynchronous so that my GUI doesn't stop.

      Normally one create threads for asynchronous activities, but as I'm calling a member function of myClass ; whats option do I've ? Is their a simpler way of doing this in QT ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @pingal
      https://doc.qt.io/qt-5.12/qml-workerscript.html

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pingal
        wrote on last edited by pingal
        #3

        Okay so I've created a WorkerScript like below:

        WorkerScript.onMessage = function(message){
        
           //Some JS code
        
        myClass.recvFromQML (...) // This is not recognized in the new thread 
        

        I've exposed this class as below in my main.cpp

        Engine.rootContext()->setContextProperty("myClass", &obj);
        

        How can i pass myClass.recvFromQML() to script.mjs

        raven-worxR 1 Reply Last reply
        0
        • P pingal

          Okay so I've created a WorkerScript like below:

          WorkerScript.onMessage = function(message){
          
             //Some JS code
          
          myClass.recvFromQML (...) // This is not recognized in the new thread 
          

          I've exposed this class as below in my main.cpp

          Engine.rootContext()->setContextProperty("myClass", &obj);
          

          How can i pass myClass.recvFromQML() to script.mjs

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @pingal
          whats the type of the "myClass" class exactly and what do you want to do with it?

          have you tried passing it as parameter like in the doc example?

          myWorker.sendMessage({ 'myClass': myClass })
          

          and in the worker:

          WorkerScript.onMessage = function(message) {
              // ... long-running operations and calculations are done here
              WorkerScript.sendMessage({ 'myClass': 'myClass = ' + message.myClass })
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pingal
            wrote on last edited by pingal
            #5

            myClass is derived from a QObject which has a member function i.e. recvFromQML() whose function is to send some data using a socket. The process is slow therefore I want it to happen async once invoked from QML.

            So far I've tried passing it as parameter like below in my QML file

            myWorker.sendMessage({'someString': x, 'myClass':myClass});
            

            and in the worker:

            WorkerScript.onMessage = function(message){
            
            var someStr = message.someString;   // This works
            message.myClass.recvCmdFromQml(...) // This doesn't
            
            }
            

            QT returns the following error

            qrc:/script.mjs:13: TypeError: Cannot call method 'recvCmdFromQml' of undefined

            P.S: The documentation says "the script.mjs in the above example cannot access the properties, methods or other attributes of the QML item, nor can it access any context properties set on the QML object through QQmlContext".
            So how would one call the invokable function from within script.mjs

            raven-worxR 1 Reply Last reply
            0
            • P pingal

              myClass is derived from a QObject which has a member function i.e. recvFromQML() whose function is to send some data using a socket. The process is slow therefore I want it to happen async once invoked from QML.

              So far I've tried passing it as parameter like below in my QML file

              myWorker.sendMessage({'someString': x, 'myClass':myClass});
              

              and in the worker:

              WorkerScript.onMessage = function(message){
              
              var someStr = message.someString;   // This works
              message.myClass.recvCmdFromQml(...) // This doesn't
              
              }
              

              QT returns the following error

              qrc:/script.mjs:13: TypeError: Cannot call method 'recvCmdFromQml' of undefined

              P.S: The documentation says "the script.mjs in the above example cannot access the properties, methods or other attributes of the QML item, nor can it access any context properties set on the QML object through QQmlContext".
              So how would one call the invokable function from within script.mjs

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @pingal
              okay,

              P.S: The documentation says

              thats why i suggested passing the instance as a parameter, but this unfortunately doesnt seem to work.

              Anyway you can move the threading to the C++ side and invoke a signal on the object once you task has finished.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pingal
                wrote on last edited by
                #7

                I've created a QThread object inside myClass and started the thread upon invoking myClass.recvFromQML(..). It solved the freezing issue.

                Thanks

                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