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. Pass a QML Object to workerScript
QtWS25 Last Chance

Pass a QML Object to workerScript

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.5k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    Hi,

    I have registered a type called 'Client' defined in C++ :

    qmlRegisterType<Client>("com.mycompany.client", 1, 0, "Client");
    

    I can Instantiate a Client{ } in QML and call Q_INVOKABLES read Q_PROPERTIES etc...

    Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.

    How can a pass 'myClient' to my WorkerScript ?

    Client {
     id:myClient
    }
    Button{
     onClicked: {
       var msg = {'client':myClient };
         connect_worker.sendMessage(msg) // here i want to pass myClient to Connect_Worker.js
    }
    }
    
        WorkerScript{
            id:connect_worker
            source: "Connect_Worker.js"
        }
    

    //Connect_Worker.js

    WorkerScript.onMessage = function(msg) {
        var co = msg.client.connectToSftpServer()
        return co
    }
    
    

    Error : TypeError: Cannot call method 'connectToSftpServer' of undefined

    How to correctly pass a QML Object to workerScript ?

    Thank you in advance.

    JKSHJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      Hi,

      I have registered a type called 'Client' defined in C++ :

      qmlRegisterType<Client>("com.mycompany.client", 1, 0, "Client");
      

      I can Instantiate a Client{ } in QML and call Q_INVOKABLES read Q_PROPERTIES etc...

      Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.

      How can a pass 'myClient' to my WorkerScript ?

      Client {
       id:myClient
      }
      Button{
       onClicked: {
         var msg = {'client':myClient };
           connect_worker.sendMessage(msg) // here i want to pass myClient to Connect_Worker.js
      }
      }
      
          WorkerScript{
              id:connect_worker
              source: "Connect_Worker.js"
          }
      

      //Connect_Worker.js

      WorkerScript.onMessage = function(msg) {
          var co = msg.client.connectToSftpServer()
          return co
      }
      
      

      Error : TypeError: Cannot call method 'connectToSftpServer' of undefined

      How to correctly pass a QML Object to workerScript ?

      Thank you in advance.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      See the documentation of WorkerScript.sendMessage(): http://doc.qt.io/qt-5/qml-workerscript.html#sendMessage-method :

      The message object may only contain values of the following types:

      • boolean, number, string
      • JavaScript objects and arrays
      • ListModel objects (any other type of QObject* is not allowed)

      All objects and arrays are copied to the message. With the exception of ListModel objects, any modifications by the other thread to an object passed in message will not be reflected in the original object.

      Unfortunately, you cannot pass an arbitrary QML object or C++ QObject to WorkerScript.

      One possible solution is for QML to send a signal back to C++ and create the new thread in C++.

      @LeLev said in Pass a QML Object to workerScript:

      Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.

      Make sure your Client object is thread-safe!

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

      ODБOïO 1 Reply Last reply
      2
      • JKSHJ JKSH

        See the documentation of WorkerScript.sendMessage(): http://doc.qt.io/qt-5/qml-workerscript.html#sendMessage-method :

        The message object may only contain values of the following types:

        • boolean, number, string
        • JavaScript objects and arrays
        • ListModel objects (any other type of QObject* is not allowed)

        All objects and arrays are copied to the message. With the exception of ListModel objects, any modifications by the other thread to an object passed in message will not be reflected in the original object.

        Unfortunately, you cannot pass an arbitrary QML object or C++ QObject to WorkerScript.

        One possible solution is for QML to send a signal back to C++ and create the new thread in C++.

        @LeLev said in Pass a QML Object to workerScript:

        Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.

        Make sure your Client object is thread-safe!

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @JKSH thank you.

        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