Dynamic create object



  • 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!



  • 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.



  • 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.


  • Moderators

    @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)
    


  • 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.


  • Moderators

    @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}';
       }
    }
    


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.