Important: Please read the Qt Code of Conduct -

C++ listmodel with

  • Hi all
    ListView has to devide the listmodel to sections
    i need to create a c++ list model (of QString data) that support this
    how can i do that


  • QStringList coupled with setContextProperty to use it in QML side (perhaps with some javascript for displaying the string you select).

    Otherwise you can use a hidden ListView and use its model to display the one you need.

  • Thanks for your answer.

    maybe i was not clear.
    i have a cpp model using QAbstractListModel and i use it in QML files and it works ok
    i want to have the section attribute that ListView have in QML and i dont know how to implement it in the QAbstractListModel
    do i need to implement the headerData method in the QAbstractListModel??

  • Just to clarify in QML holds the name of the property that is the basis of each section.

    If you look at the example in "QML Data Models": it shows that the roles in the c++ model is used in QML. In the example the roles are "type" and "size". So if you want to create a section base on "type" you would alter the QML example (in the link) to:
    @ListView {
    width: 200; height: 250
    anchors.fill: parent

     model: myModel
     delegate: Text { text: "Animal: " + type + ", " + size } type


    Hope that is what you are looking for. The above snippet is untested of course

  • I have the same problem and i found solution - you should write "modelData.<propertyName>"

  • I have the same problem and i found solution - you should write "modelData.<propertyName>"

Log in to reply