Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML Types from C++ terrible things: singletons, lists and constants



  • 1. Singletons with lists. If there is QML type with QQmlListProperty eg. MyType registered as qmlRegisterSingletonType<MyType>("org.examples", 1, 0, "MyType") which has QmlListProperty<MyListElement> - myList. What syntax i should use at QML side to assign something to MyType.myList?

    Syntax like that don't works:

    MyType.myList = [
      MyListElement {
        id: elem1
      } 
    ]
    
    

    It works that way (Qt.createQmlComponent also can be used):

    Component.onCompleted: {
        var o = Qt.createQmlObject('import org.examples 1.0; MyListElement {}')
        MyType.myList.push(o);
    }
    

    But there is no acess to MyListElements by their id's.

    2. Constants: i see no way how to define QML type (non-singleton or singleton) at C++ side with constant properties that should be initialized at QML side when creating object


  • Qt Champions 2018

    I believe that should work for singletons with QQmlListProperty:

    Binding {
        target: MyType
        property: "myList"
        value: [
          MyListElement {
            id: elem1
          } 
        ]
    }
    


  • @GrecKo no, that don't works: qrc:/main.qml:25 Cannot assign multiple values to a singular property


Log in to reply