Solved What method needs to be invoked to change the value of the 'role' parameter of the QAbstractListModel::data method?
-
QVariant QAbstractListModel::data(const QModelIndex &index, int role) const
QVariant ReaderModel::data(const QModelIndex &index, int role) const { if(index.row() < rowCount()) switch(role){ case IdRole: return m_readers.at(index.row())->id(); case PasswordRole: return m_readers.at(index.row())->password(); case RecordRole: return m_readers.at(index.row())->record(); default: return QVariant(); } return QVariant(); } QHash<int, QByteArray> ReaderModel::roleNames() const { static const QHash<int, QByteArray> roles{ {IdRole, "id"}, {PasswordRole, "password"}, {RecordRole, "record"} }; return roles; }
#ifndef READERMODEL_H #define READERMODEL_H #include <QAbstractListModel> #include <QJsonValueRef> #include <QVariant> #include "reader.h" #include "record.h" class ReaderModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(Reader* me READ me WRITE setMe NOTIFY meChanged) public: enum ReaderRole { IdRole = Qt::DisplayRole, //0 PasswordRole = Qt::UserRole, RecordRole }; Q_ENUM(ReaderRole) ReaderModel(QObject *parent = nullptr){Q_UNUSED(parent);} int rowCount(const QModelIndex & = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash<int, QByteArray> roleNames() const; Q_INVOKABLE QVariantMap get(int row) const; Q_INVOKABLE void append(const QString &id, const QString &password); Q_INVOKABLE void set(int row, const QString &id, const QString &password); Q_INVOKABLE void remove(int row); Q_INVOKABLE Reader *me() const; Q_INVOKABLE void setMe(Reader *r); private: QList<Reader*> m_readers; Reader *me_; signals: void meChanged(); }; #endif // READERMODEL_H
-
You want to display id, password, record from QML, right? Then you need something like this:
ListView { model: yourReaderModel delegate: Text { height: 30; width: 150 text: model.id + ", " + model.password + ", " + model.record } }
If it does not work, use
modelData
instead ofmodel
. -
You want to display id, password, record from QML, right? Then you need something like this:
ListView { model: yourReaderModel delegate: Text { height: 30; width: 150 text: model.id + ", " + model.password + ", " + model.record } }
If it does not work, use
modelData
instead ofmodel
.