Skip to content
  • 1 Votes
    4 Posts
    704 Views
    jeremy_kJ

    Do these objects output by the factory have a lifetime that needs to exceed the QML component used for the object's controls? If not, I would make the object a QML (not Quick) type, and have the component that displays the controls also instantiate the object.

    Even if they do have lifetimes that don't match the UI portion, the controls can be thin wrappers around the objects that really implement the backend. In the example below RadioBackend and TapeBackend are QObject derived classes that export Q_INVOKABLE functions and properties for communication with the UI. Register them with one of the qmlRegister* functions, depending on the desired interface.

    For example:

    Radio.qml:

    Rectangle { color: control.colorTheme RadioBackend { id: control } Row { Button { onClicked: control.doAction() } Button { onClicked: control.doOtherAction() } } }

    TapeDeck.qml:

    Rectangle { color: control.colorTheme TapeBackend { id: control } Button { onClicked: control.doTapeAction() } }

    main.qml:

    Loader { property MediaType mediaType source: mediaType === MediaType.Radio ? "file://Radio.qml" : "file://TapeDeck.qml" }
  • Use c++ factory classes in qml

    Unsolved QML and Qt Quick
    6
    0 Votes
    6 Posts
    2k Views
    SGaistS

    Then why not make your manager a context object of your QQmlEngine instance and then use it in your QML code to retrieve the objects you need ?