Important: Please read the Qt Code of Conduct -

How to get content of a single row based on a C++ QList model in QML?

  • Hi,

    I basically would like to do what can be found here as second answer:

    The main difference is that I am trying to use a QList based model in C++.

    Somehow I can access e.g. the content of "name" in the delegate, but further down this only works using a QML based model, like shown on stackoverflow.

    Could you please tell me what the problem is and how to solve this?

    Thank you very much!


    import QtQuick 2.0
    import QtQuick.Layouts 1.3
    ListView {
        id: list
        implicitWidth: 250
        implicitHeight: 250
        clip: true
        model: myModel
        delegate: Component  {
            Item {
                width: parent.width
                height: 40
                Row {
                    Text { text: name } // usage of "name" works here
                    Text { text: "\t" }
                    Text { text: model.modelData.color}
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        list.currentIndex = index
        highlight: Rectangle {
            color: 'grey'
            Text {
                anchors.centerIn: parent
                text: 'Hello ' + model.get(list.currentIndex).name //"name" does not work here
                color: 'white'
        focus: true
        onCurrentItemChanged: console.log(model.get(list.currentIndex).name + ' selected') // does not work


    #include <QGuiApplication>
    #include <qqmlengine.h>
    #include <qqmlcontext.h>
    #include <qqml.h>
    #include <QtQuick/qquickitem.h>
    #include <QtQuick/qquickview.h>
    #include "dataobject.h"
       This example illustrates exposing a QList<QObject*> as a
       model in QML
    int main(int argc, char ** argv)
        QGuiApplication app(argc, argv);
        QList<QObject*> dataList;
        dataList.append(new DataObject("Item 1", "red"));
        dataList.append(new DataObject("Item 2", "green"));
        dataList.append(new DataObject("Item 3", "blue"));
        dataList.append(new DataObject("Item 4", "yellow"));
        QQuickView view;
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
        return app.exec();


    #ifndef DATAOBJECT_H
    #define DATAOBJECT_H
    #include <QObject>
    class DataObject : public QObject
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
        DataObject(QObject *parent=0);
        DataObject(const QString &name, const QString &color, QObject *parent=0);
        QString name() const;
        void setName(const QString &name);
        QString color() const;
        void setColor(const QString &color);
        void nameChanged();
        void colorChanged();
        QString m_name;
        QString m_color;
    #endif // DATAOBJECT_H


    #include <QDebug>
    #include "dataobject.h"
    DataObject::DataObject(QObject *parent)
        : QObject(parent)
    DataObject::DataObject(const QString &name, const QString &color, QObject *parent)
        : QObject(parent), m_name(name), m_color(color)
    QString DataObject::name() const
        return m_name;
    void DataObject::setName(const QString &name)
        if (name != m_name) {
            m_name = name;
            emit nameChanged();
    QString DataObject::color() const
        return m_color;
    void DataObject::setColor(const QString &color)
        if (color != m_color) {
            m_color = color;
            emit colorChanged();

  • Moderators

    Highlight is not part of the delegate so it does not get to see it's properties. You can rewrite your code to include the highlight in your delegate, though.

Log in to reply