Solved 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:
https://stackoverflow.com/questions/9400002/qml-listview-selected-item-highlight-on-clickThe 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(); } }
-
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.