Nominate our 2022 Qt Champions!

Generate an XML file using the QML Listmodel

  • Hi All,

    I would like to generate a xml file (either in SAX or DOM format whichever is convenient) using a Qt C++ class and the data for the creation of this xml file need to fetched from the QtQuick List model(data model).
    When the data has been extracted from the data model it will need to be written to a file.
    Please suggest possible solutions for the same.


  • Moderators

    Hi @Ravikiran-V-N,
    The are several ways access a ListModel from C++. In each case you will need to get access to ListModel object which can be done by using findChild provided you have set objectName to the ListModel. Once that is done,

    • Access ListModel functions . The function of interest here would be get. You can follow this document to understand how to invoke QML functions from C++.

    • ListModel is an QAbstractItemModel so once you get access of ListModel on C++ side, cast it to QAbstractItemModel and use it functions for eg. data() to access the ListModel data.

    Later use QXmlStreamWriter to create XML file. There's QtXml module too but it is not supported anymore.

    Hope this gets you going :)

    Btw. can you explain why you are preferring populating ListModel instead of C++ models directly ? As anyway at the end you are coming to C++ for further processing.

Log in to reply