How to access data from Qabstractlistmodel QList<object> to Qml instead of call each rolecall
-
attached code what i have done.
here in qml file insread of call each role can we have whole object data at one time it will reduce my calls from qml to qt to get data.
please let me if any methods were there instead of going role based.event.h
namespace model { class Event { Q_GADGET Q_DECLARE_TR_FUNCTIONS(Event) public: Event(uint16_t Code, uint16_t Id, uint16_t Type, uint16_t Adress, uint64_t TimeStamp, ) : m_Code{Code}, m_Id{Id}, m_Type{Type}, m_Adress{Adress}, m_TimeStamp{TimeStamp}, { } uint16_t getCode() const { return m_Code; } uint16_t getId() const { return Id; } uint16_t getType() const { return m_Type; } uint16_t getAdress() const { return m_Adress; } uint64_t getTimestamp() const { return m_TimeStamp; } private: uint16_t m_Code= 0; uint16_t m_Id = 0; uint16_t m_Type= 0; uint16_t m_Adress = 0; uint64_t m_TimeStamp = 0; }; } // namespace model
datamodel.h
#include "event.h" #include <QVector> #include <QAbstractListModel> #include <QSortFilterProxyModel> #include <QList> #include <QMutex> #include <QVariant> namespace model { /** * The EventDataModel class. */ class EventDataModel : public QAbstractListModel { Q_OBJECT public: /** * @brief Data role enums. */ enum EventRoles { IdRole = Qt::UserRole + 1, TimeStampRole, CodeRole, TypeRole, AddressRole, }; Q_ENUM(EventRoles) DataModel(QObject* const parent = nullptr); ~DataModel() override = default; static EventDataModel& getInstance(QObject* const parent = nullptr) { static EventDataModel instance{}; return instance; } // Re-implemented from QAbstractListModel. /** * @brief Returns the role name look-up table. * * @return look-up table. */ QHash<int, QByteArray> roleNames() const override { return m_roles; } /** * @brief Returns number of rows in the model. * * @param[in] parent Unused (provided to match function signature of base class function). * @return rows Number of rows. */ int rowCount(const QModelIndex& parent = QModelIndex()) const override { Q_UNUSED(parent); return m_events.count(); } /** * @brief Create index from row and column number and return it. * * @param[in] row The row to give the index of. * @param[in] column The column to give the index of. * @param[in] parent Unused (provided to match function signature of base class function). * @return index The index of the row/column. */ QModelIndex index(int row, int column = 0, const QModelIndex& parent = QModelIndex()) const override; /** * @brief Access data in the model. * * @param[in] index The index of the entity. * @param[in] role The role to return data for. * @return data The data. */ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; void addEvent(Event &faultEvent); void removeExistingEvent(uint64_t Id ); void RemoveEvent(const uint8_t moduleid ); signals: private: QMutex m_mutex; ///< Synchronization object. @todo investigate if a mutex is needed. QList<Event> m_events; ///< List of all modules. QHash<int, QByteArray> m_roles; ///< Role lookup table. ///counts }; } // namespace model
datamodel.cpp
namespace model { DataModel::DataModel( QObject* const parent) : QAbstractListModel{parent} , m_roles{} { // initialise roles m_roles[IdRole] = "id"; m_roles[TypeRole] = "type" ; m_roles[TimeStampRole] = "timeStamp" ; m_roles[AddressRole] = "address" ; m_roles[CodeRole] = "code" ; } QModelIndex DataModel::index(int row, int column, const QModelIndex& parent) const { Q_UNUSED(parent); if ((row >= 0) && (row < m_events.count())) { return createIndex(row, column, (void*)&(m_events[row])); } return QModelIndex(); } QVariant DataModel::data(const QModelIndex& index, int role) const { if (!(index.isValid())) { return QVariant(); } const Event &_data= m_events[m_events.count()-index.row()-1]; switch (role) { case IdRole : return QVariant::fromValue(_data.getId()); break ; case TypeRole : return QVariant::fromValue(_data.getType()); break ; case CodeRole : return QVariant::fromValue(_data.getCode()); break ; case TimeStampRole : return QVariant::fromValue(_data.getTimestamp()); break ; case AddressRole : return QVariant::fromValue(_data.getDeviceAdress()); break ; default: return QVariant(); } } void DataModel::addEvent(Event &event) { QMutexLocker locker(&m_mutex); beginInsertRows(QModelIndex(), 0, 0); m_events << event; endInsertRows(); } } void DataModel::removeExistingEvent(uint16_t Id ) { QList<Event>::iterator event; for (event= m_events.begin(); event!= m_events.end(); ++event) { if ((event->getId() == Id)) { beginRemoveRows(QModelIndex(), 0, 0); m_faults.erase(fault); emit endRemoveRows(); } } } }
Qml files to access data
view.qml
ListView { id: deviceList width: parent.width anchors.top: recfortabs.bottom anchors.topMargin: 4 height:800 anchors.left: parent.left spacing: 4 model: DataModel clip: true delegate: Rectangle { height: 100 width: 800 Text { id: serialNo text: model.Id color: main.colors.listFont font.pixelSize: 24 anchors.top: standardList.top anchors.topMargin: 28 anchors.left: standardList.left anchors.leftMargin: 24 } Text { id: type color: main.colors.white text:model.type font.pixelSize: 24 anchors.top: standardList.top anchors.topMargin: 24 anchors.left: standardList.left anchors.leftMargin: 32 anchors.right: timeReported.left anchors.rightMargin: 24 wrapMode: Text.WordWrap font.family: main.fontBold font.weight: Font.Bold //font.bold: true } Text { id: timeReported text: model.timeStamp color: main.colors.white font.pixelSize: 24 anchors.top: zoneDetails.top anchors.right: openCloseButton.left anchors.rightMargin: 32 font.family: main.fontBold font.weight: Font.Bold visible: true } ..... } }
-
@sainaresh
I could say that what you have done is correct in terms of the usage of AbstractListModel.This Qt example uses a way that's similar to how you have done it in your example https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/models/abstractitemmodel/model.h?h=5.15
If you still want to access the whole data in one object, I would suggest to wrap it inside a QObject subclass with Q_PROPERY's and use only one role and return that object from the model class in data() method.
-
@sainaresh
I could say that what you have done is correct in terms of the usage of AbstractListModel.This Qt example uses a way that's similar to how you have done it in your example https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/models/abstractitemmodel/model.h?h=5.15
If you still want to access the whole data in one object, I would suggest to wrap it inside a QObject subclass with Q_PROPERY's and use only one role and return that object from the model class in data() method.
thanks for the Replay
I am new Qt/Qml could you provide any example for wrap it inside a QObject subclass with Q_PROPERY's and use only one role and return that object from the model class in data()
if i am loading dynamic data Qlist which method will be the fastest method to get the Data in qml (intract QML with each role or QObject subclass with Q_PROPERY's with one role)
note:- if have more than 15 roles