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 create object

Dynamic create object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 3.4k 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.
  • MaxKazakovM Offline
    MaxKazakovM Offline
    MaxKazakov
    wrote on last edited by
    #1

    Hi everybody

    Could you please tell me how can I dinamicly create one of QtQuick object from standart modules, for example Button from QtQuick.Controls?

    I have realized that there are 2 way to create object dynamicly:

    1. Qt.createComponent and Qt.createObject
    2. Qt.createQmlObject

    1st is usefull if I need to create object from my own qml file.

    2nd allows create Button object for example like:

    var newObject = Qt.createQmlObject('import QtQuick 2.0; Button {color: "red"; width: 20; height: 20}',
                                       parentItem,
                                       "");
    

    But it is the same if I just create my own qml file with code:

    import QtQuick 2.0; Button {color: "red"; width: 20; height: 20}
    

    and use the 1nd variant.

    I cant believe there is no way just write something like

    var button = Qt.createObject(Button)
    

    Thanks in advance!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      peteritv
      wrote on last edited by
      #2

      Hi Max,

      Qt.CreateObject needs a named instance as a parameter, and not just a type.

      So

      Button { id: myButton }
      ....
      var newButton = Qt.CreateObject(myButton)
      

      should do the trick.

      1 Reply Last reply
      1
      • MaxKazakovM Offline
        MaxKazakovM Offline
        MaxKazakov
        wrote on last edited by
        #3

        It should be a mistake. You likely wanted to write something like:

        Button { id: myButton }
        ....
        var button = myButton.createObject(parent)
        

        Because there is no "createObject" function in "Qt" type.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @MaxKazakov The method is for Component type.
          http://doc.qt.io/qt-5/qml-qtqml-component.html#createObject-method

          So

          Component {
              id: comp
              Button { id: button }
          }
          
          var button = comp.createObject(parent)
          

          157

          1 Reply Last reply
          1
          • MaxKazakovM Offline
            MaxKazakovM Offline
            MaxKazakov
            wrote on last edited by MaxKazakov
            #5

            Yes, thank you, I know it works fine.

            I just interesting in the only one thing:

            I can use this to create any type of custom object, for example MyType instance:

            component = Qt.createComponent("MyType.qml");
            myobj = component.createObject(appWindow, {"x": 100, "y": 100});
            

            It's Ok, but how can I use similar syntax to create any type of object imported from module(for example QtQuick, or QtQuick.Controls or MyControls module), not from any qmlfile? Because as I know a module is just a way to group a few qmlfiles(also js files, cpp classes and etc). It means that it's not must be difficult to use something like:

            component = Qt.createComponent("NameOfQmlTypeFromModule"); 
            obj = component.createObject(appWindow, {"x": 100, "y": 100});
            

            Please corrent me if I'm wrong! Thank you!

            PS. As I know Qt.createComponent works with file names(urls) from qml recources. So I don't tell about same approach(Qt.createComponent), just about similar.

            PSS. There integerstring example in QML documentation:

            import QtQuick 2.0
            
            Item {
                id: container
                width: 300; height: 300
            
                function loadButton() {
                    var component = Qt.createComponent("Button.qml");
                    if (component.status == Component.Ready) {
                        var button = component.createObject(container);
                        button.color = "red";
                    }
                }
            
                Component.onCompleted: loadButton()
            }
            

            This is what I mean - creating instance of Button type which contained into some module. This example do not work of course if we don't have "Button.qml" file in our qml resources.

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @MaxKazakov Because createComponent can only create a type only when a QML file is specified what you are trying is not possible. So as you have already figured out you have the only option of using createObject or createQmlObject.
              You can write a JS function which when called can return the appropriate type. Something like:

              function getType(type) {
                 if(type=="button") { 
                      return 'import QtQuick 2.0; Button {color: "red"; width: 20; height: 20}';
                 }
              }
              

              157

              1 Reply Last reply
              1
              • MaxKazakovM Offline
                MaxKazakovM Offline
                MaxKazakov
                wrote on last edited by
                #7

                OK, thank you.
                I should to know much more about modules, seems my I understanding of them is wrong.

                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