Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get content of a single row based on a C++ QList model in QML?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 352 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    Hi,

    I basically would like to do what can be found here as second answer:
    https://stackoverflow.com/questions/9400002/qml-listview-selected-item-highlight-on-click

    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!

    qml:

    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
                        console.log(list.currentIndex)
                    }
    
                }
            }
        }
        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
    }
    

    main.cpp:

    #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
    */
    
    //![0]
    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;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    //![0]
    
        view.setSource(QUrl("qrc:view.qml"));
        view.show();
    
        return app.exec();
    }
    

    dataobject.h:

    #ifndef DATAOBJECT_H
    #define DATAOBJECT_H
    
    #include <QObject>
    
    //![0]
    class DataObject : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
    //![0]
    
    public:
        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);
    
    signals:
        void nameChanged();
        void colorChanged();
    
    private:
        QString m_name;
        QString m_color;
    //![1]
    };
    //![1]
    
    #endif // DATAOBJECT_H
    

    dataobject.cpp

    #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();
        }
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved