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. Can I using Qt.createComponent() in WorkerScript function?
QtWS25 Last Chance

Can I using Qt.createComponent() in WorkerScript function?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 4 Posters 3.6k 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.
  • P Offline
    P Offline
    Pong_Cpr.E.
    wrote on last edited by
    #1

    I'm new in Qt quick app. (QML)

    I wanna create dynamic image table(like in Samegame example) but it using thread to make real-time create by using workerScript.

    How to using WorkerScript by Qt function can be passed between the new thread and the parent thread? or can import Qt func in javascript?

    example
    @//main.qml
    import QtQuick 1.0

    Rectangle {
    width: 800; height: 600

    WorkerScript {
        id: myWorker
        source: "script.js"
    }
    
    Item {
         id: container
         anchors.fill: parent
    }
    
    Component.onCompleted: myWorker.sendMessage({ 'container':container })
    

    }@

    @//script.js
    WorkerScript.onMessage = function(message) {
    // error occur in this below line with TypeError: Result of expression 'Qt.createComponent' [undefined] is not a function.
    var component = Qt.createComponent("Imgbox.qml");
    if (component.status == Component.Ready) {
    var img = component.createObject(message.container);
    }
    }@

    @//Imgbox.qml
    import QtQuick 1.0

    Image {
    id: img
    source: "./img.jpg"; clip: true
    }@

    Thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blam
      wrote on last edited by
      #2

      No, the JavaScript code in a WorkerScript is run in a separate thread, and QML objects should only be created in the main thread, in the same thread as the QML engine.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kahon
        wrote on last edited by
        #3

        Are you sure?
        I need to do that, and I have watched some code with QML code in Javascript. But you need to configure .qmlproject file. Really I don't know how to do it. But I'm figuring out.
        So, Is there someone who got this?

        Thanks,
        Fernando.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chriadam
          wrote on last edited by
          #4

          There is no QQmlEngine in the WorkerScript thread, only a JavaScript engine. Attempting to create a component in a thread which has no QQmlEngine cannot succeed. You can create QObjects in a WorkerScript thread, but not QML items (well, you technically could create a QQuickItem, but it wouldn't be usable).

          You can interact with certain QML items from within functions in a WorkerScript (e.g., ListModel items are a good example) but some magic happens behind the scenes to allow that.

          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