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. What method needs to be invoked to change the value of the 'role' parameter of the QAbstractListModel::data method?

What method needs to be invoked to change the value of the 'role' parameter of the QAbstractListModel::data method?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 328 Views 1 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by
    #1

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

      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 of model.

      (Z(:^

      1 Reply Last reply
      1

      • Login

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