QML List fetches DisplayRole from model; how can I change that?

  • I have some QML (that I did not write and to be honest do not understand, but nonetheless need to maintain) code that looks a lot like this:

    Item {
    anchors {
    fill: parent
    margins: 30

        List {
            id: recentList
            header: qsTr("Open Item")
            model: recentModel
            anchors {
                left: parent.left
                right: parent.right
                top: parent.top

    The recentModel is a proper QT model, and what gets displayed looks like the returned value from a call to data with the role parameter set to DisplayRole. I'd like it to fetch a different string, as if it was calling data with otherRole. I've been rooting around the QML documentation, but selecting with data role doesn't seem to come up. Is it possible?

  • You must explicitly create mapping of role names in QAbstractItemModel::roleNames() for QML to access data in the model. Then QML will know how to map accessed property to role when calling data().
    Is this sufficient help for you?

    "Doc is here":http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel, but it looks like lacking sample code :/

  • bq. Is this sufficient help for you?

    It may well be, thank you. I am using 4.8 to maintain a Solaris compatibility, but the function does exist in 4.8

  • bq. "Doc is here":http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel [qt-project.org], but it looks like lacking sample code :/

    The sample code installs on your computer with Qt. On my drive it is:


    The examples in the source code installed with Qt are incredibly useful. I wasted hours searching for sample code on the Internet that was already on my hard drive.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.