Dynamic creation of a "standard" QML object (not from my own .qml file)



  • All the articles/posts I found show the use of Qt.createComponent() to load a Component from a QML url, and then to instantiate it:
    @
    component = Qt.createComponent(sourceQml);
    myControl = component.createObject(parentElement);
    @

    I want to dynamically create an instance of a "known" QML object that I don't have the path to. In my case it's RadioButton from the Qt-Components library but it can be anything else.

    What's the syntax for it?





  • That would help me if I wanted to code the QML itself on the fly.
    But I want to use an existing QML object. Like <Image> or <Text>.



  • @
    var newObject = Qt.createQmlObject('import QtQuick 1.0; Rectangle {}', parentItem, "dynamicSnippet1");
    @

    This will create a Rectangle item.



  • That's a cool way of doing it. Thanks!



  • Another option I've heard suggested is:

    @
    Component {
    id: component
    RadioButton {}
    }
    ...
    myControl = component.createObject(parentElement);
    @



  • I ended up using a wrapper QML file anyway for other needs, but I will actually need to update it now to another component. So I think I'll try this new method too. Thanks!


Log in to reply
 

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