Important: Please read the Qt Code of Conduct -

Expose variable to QML AND C++

  • I want to expose a variable declared in the main procedure to QML.
    I do this with the following
    XmlType xmlFile;
    viewer.rootContext()->setContextProperty("xmlFile", &xmlFile);

    • declaration of type in xmltype.cpp/.h
    • declaration of variable in main.cpp
    • presentation of variable in show.qml
    • want to alter the data in change.cpp

    But how to access this variable from another class?

  • Hello,

    if you want to access the property of XML type, you had to use the Properties like :
    @Q_PROPERTY(QString dateHeure READ getDateHeure WRITE setDateHeure NOTIFY dateHeureChanged)@

    and of course implements the functions, then you can use them in your QML file with the syntax xmlFile.dateHeure, and modify the values as well.

    A special syntax is used for lists (see declarativeListProperty),
    or you can use models....

  • Thank you for your code snippet

  • Can you explain how to make a list available in QML.
    You described that I can use the qdeclarativeListProperty or a model.

    In my xmlFile-Object exists a List which you get when you call the function getParameters within.
    The return type is a ListModel, as described in "here":

    I thought on something like this in QML:

    ListView {
    model: xmlFile.getParamters()
    Item {
    Text {
    text: xmlFile.getParameterName

    The list holds Parameter-Objects (in the link above these where fruititem-Objects) which have several fields like name (getParameterName) or value.
    I can't adapt the example from the link because I didn't define my List in the main.cpp but in the xmlFile-Object.

  • Hi,

    in the QML you don't have to use the function, but the name used in the Q_PROPERTYdeclaration :

    xml.parameter, or xmlFile.parameterName, in the example
    @names[NameRole] = "name";@ the parameterName is "name"

    However the example is a good one!

    you could add the @ view.rootContext()->setContextProperty("fruitsModel", createModel());@
    where you want even if it's not in the main, as soon as you acess to the declarativeView, it will also works

  • OK, it is clear to me that I have do use the defined names in the Q_PROPERTY.

    How to set the Q_PROPERTY in the main.cpp for the list?

    The list comes from the Function: xmlFile.getParameters() and holds Parameter-Object.
    The delegates in the ListView shoud display name and value of each object in the list.
    Further the QML-File do not know about the Parameter-Type.

    Is this the result:
    Q_PROPERTY(QString parameter READ xmlFile.Parameter() )
    Q_PROPERTY(QString name READ xmlFile.Parameter().name())

    Or can I do something like that:

    ctxt->setContextProperty("parameters", xmlFile.getParameters());
    ctxt->setContextProperty("name", xmlFile.getParameters().name());

    I think I also should expose the Parameter-Type. Is this right?

  • I got it working with the following lines:

    ctxt->setContextProperty("parameters", xmlFile.getParameters());

    ListView {
    model: parameters
    Item {
    Text {
    text: name

  • Now I can add Elements with appendRow().
    But when I want to delete all the list entries with clear() strange things happen:

    I do the following:

    addRow() x 6


    addRow() x 4

    Now I have 6 elements in the list:
    elements 1 to 4 are from 3. , elements 5 to 6 are from 1.
    How can this be?

    If I first add 4 elements, clear, and then add 6 elements I get 7 elements in my ListView:
    elements 1 to 6 are from the addRow() after the clear and element 7 is from the first addRow() before the clear().

    I hope this is not too confusing.

    Can it be, that there is some kind of memory leak due to the setContextProperty() thing?

  • I found out that flicking/scrolling the ListView erases the wrong items.
    This doesn't solve my problem but maybe helps unterstanding it.
    The rowCount() also gives the correct number of items. Only the view is not correct.

    Is there someone with the same problem?

Log in to reply