Important: Please read the Qt Code of Conduct -

Exposing a dynamic set of properties to Qml

  • Hello !

    I'm a trying to do the following with Qml :
    My project is an application with a Qml interface around some non-Qt modules (loaded at runtime).
    The application and the modules communicate through PropertyContainers which are dictionaries of abstract data (the equivalent of a QVariantMap but with a different implementation).
    I want to expose these dictionaries to Qml, without writing C++ code specific to the keys in these dictionaries (new modules and qml interfaces must be developed without rebuilding the application).

    My first implementation relies on QAbstractListModel. I can attach any of these PropertyContainers to the model and a Qml ListView will display a simple property editor. This implementation has some limitations: it only works for Qml Items meant to use a model (Repeater, ListView) and it doesn't allow binding.

    Ideally I would like to have each dictionary entry available as a property from Qml.
    For example a PropertyContainer with the following properties:

    • text = "Hello World"
    • color = "red"
    • width = 100

    Would be wrapped in a "props" QObject which can be accessed from Qml like this

    Rectangle {
        color: props.color
        width: props.width
        TextField {
            text: props.text

    Since using QObject's dynamic properties with the function setProperty() doesn't expose the properties to Qml. I am contemplating writing a wrapper QObject class implementing its own setters/getters and using QMetaObjectBuilder to generate a per instance metaObject listing all the dictionary keys as properties.

    Building my own metaObject seems like overkill, risky and prone to cause a lot of problems when updating Qt so I would like to know a few things first.

    • Is there a simpler way to do what I'm trying to do?
    • Why is QMetaObjectBuilder not part of the public API? Is it really a bad idea to write code using this class?
    • Why is it that dynamic properties aren't available from QML? If I'm not mistaken they seem to be available from QScript. Will this change at some point?

Log in to reply