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. Dynamic QML object question
Forum Update on Monday, May 27th 2025

Dynamic QML object question

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.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.
  • cmessineoC Offline
    cmessineoC Offline
    cmessineo
    wrote on last edited by
    #1

    I can create the qml object using Qt.createQmlObject, but how do I reference the object in javascript if I want to change it's properties.

    Thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      Hi,

      The object which is created will be a QtObject-derived type. So you can reference it with a QtObject property, or (if you're using Qt5) a var property. (Note: you can also use a variant property, but I really suggest not doing that).

      eg:

      @
      Item {
      id: root
      property QtObject objref
      Component.onCompleted: {
      objref = Qt.createQmlObject("import QtQuick 2.0; Item { }", root)
      setPropsFunc()
      }

      function setPropsFunc() {
          objref.width = 100
          objref.height = 100 // etc
      }
      

      }
      @

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • cmessineoC Offline
        cmessineoC Offline
        cmessineo
        wrote on last edited by
        #3

        Thanks for the reply, that helped a lot.

        What if I was to use the code below to create my keyboard dynamically:
        @
        var keyboardObject = Qt.createObject(root, {"z": 30, "keyHeight": 40, "keyWidth":28 });
        @

        This is what it looks like if the keyboard is just added to the QML file statically:

        @
        VirtualKeyboard {
        id: vk
        z: 30
        keyHeight: 40
        keyWidth: 28

           onButtonPressed: {
               Ops.doTextOperation(op)
           }
        }
        

        @

        How can I add the onButtonPressed event dynamically using Qt.createObject? Maybe it can't be done.

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

          With Qt5 or Qt4.x?

          With Qt5 I think you could probably use a property var and initialise it with an array containing a function value (to avoid it being considered a binding).

          eg:

          @
          VirtualKeyBoard {
          property var onClickedFunction
          // ...
          }

          var keyboardObject = Qt.createObject(root, {"onClickedFunction": [function(){console.log("hi")}]});
          @

          Cheers,
          Chris.

          1 Reply Last reply
          0
          • cmessineoC Offline
            cmessineoC Offline
            cmessineo
            wrote on last edited by
            #5

            I'm using 4.8.4. I don't think it can be done. There doesn't seem to be a way I can trap that event when I create the keyboard dynamically.

            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