Using QAbstractModel
-
It depends on how you implemented it.
-
In main c++ class
Q_PROPERTY(ModelRegistration* modelRegistration MEMBER m_modelRegistration CONSTANT)
Model code:
#ifndef MODELREGISTRATION_H #define MODELREGISTRATION_H #include <QObject> #include <QAbstractListModel> struct RegistrationObject { RegistrationObject(int id, QString name, bool isDone) { m_id = id; m_name = name; m_isDone = isDone; } int m_id; QString m_name; bool m_isDone; }; class ModelRegistration : public QAbstractListModel { Q_OBJECT public: ModelRegistration(QObject *parent = nullptr); enum DeviceRoles { IdRole = 0, NameRole, IsDoneRole }; QVector<RegistrationObject> vectorDataRegistration; int rowCount(const QModelIndex& parent = QModelIndex()) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; public slots: void emitSignalUpdateModel(); protected: QHash<int, QByteArray> roleNames() const override; }; #endif // MODELREGISTRATION_H
#include "modelregistration.h" ModelRegistration::ModelRegistration(QObject *parent) : QAbstractListModel(parent) { vectorDataRegistration.append(RegistrationObject(1, "Legs (back)", false)); vectorDataRegistration.append(RegistrationObject(2, "Hips (back)", false)); vectorDataRegistration.append(RegistrationObject(3, "Back", false)); vectorDataRegistration.append(RegistrationObject(3, "Arms (back)", false)); vectorDataRegistration.append(RegistrationObject(3, "Hips (front)", false)); vectorDataRegistration.append(RegistrationObject(4, "Belly", false)); vectorDataRegistration.append(RegistrationObject(3, "Arms (front)", false)); } int ModelRegistration::rowCount(const QModelIndex &parent) const { return vectorDataRegistration.count(); } QVariant ModelRegistration::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= vectorDataRegistration.count()) return QVariant(); switch (role) { case IdRole: return vectorDataRegistration[index.row()].m_id; case NameRole: return vectorDataRegistration[index.row()].m_name; case IsDoneRole: return vectorDataRegistration[index.row()].m_isDone; } return QVariant(); } void ModelRegistration::emitSignalUpdateModel() { auto index = this->index(0); emit dataChanged(index, index, QVector<int>{0,1,2}); } QHash<int, QByteArray> ModelRegistration::roleNames() const { QHash<int, QByteArray> roles; roles[IdRole] = "id"; roles[NameRole] = "name"; roles[IsDoneRole] = "isDone"; return roles; }
-
I do not see any setData reimplementation.
Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum. -
First: fix your DeviceRoles enumeration.
-
@Mihaill said in Using QAbstractModel:
But whot me need do? Need reimplement setData ?
You roles must starts with
Qt::UserRole
(cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)So change your enumeration to:
enum DeviceRoles { IdRole = Qt::UserRole, NameRole, IsDoneRole }
-
-
Did you also fix emitSignalUpdateModel ?
-
Fix the roles values for example.
-
As I explained and @KroMignon showed you, your enum values were wrong. You hard coded the same values in that method:
@Mihaill said in Using QAbstractModel:
void ModelRegistration::emitSignalUpdateModel() { auto index = this->index(0); emit dataChanged(index, index, QVector<int>{0,1,2}); }
Change them and use your enum values.
-
It is not work, and this too not work ```
void ModelSchedule::emitSignalUpdateModel()
{
for (int q = 0; q < vectorDataRegistration.count(); q ++){
auto index = this->index(q);
QVector <int> roles;
for (int i = Qt::UserRole; i < Qt::UserRole + 3; i++) {
roles.append(i);
}
qDebug()<<"roles"<<roles;
emit dataChanged(index, index, roles);
}
} -
I do not see any setData reimplementation.
Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.@SGaist said in Using QAbstractModel:
I do not see any setData reimplementation.
Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.@KroMignon said in Using QAbstractModel:
@Mihaill said in Using QAbstractModel:
But whot me need do? Need reimplement setData ?
You roles must starts with
Qt::UserRole
(cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)So change your enumeration to:
enum DeviceRoles { IdRole = Qt::UserRole, NameRole, IsDoneRole }
This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.
As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?
-
@SGaist said in Using QAbstractModel:
I do not see any setData reimplementation.
Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.@KroMignon said in Using QAbstractModel:
@Mihaill said in Using QAbstractModel:
But whot me need do? Need reimplement setData ?
You roles must starts with
Qt::UserRole
(cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)So change your enumeration to:
enum DeviceRoles { IdRole = Qt::UserRole, NameRole, IsDoneRole }
This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.
As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?
@GrecKo said in Using QAbstractModel:
This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.
AFAIK, the engine will request the role names which shall be associated with integer values so that when requesting them, the model does know what to return. Thus these values are still important to properly set otherwise your model will start serving wrong information to the view.
-
@SGaist said in Using QAbstractModel:
I do not see any setData reimplementation.
Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.@KroMignon said in Using QAbstractModel:
@Mihaill said in Using QAbstractModel:
But whot me need do? Need reimplement setData ?
You roles must starts with
Qt::UserRole
(cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)So change your enumeration to:
enum DeviceRoles { IdRole = Qt::UserRole, NameRole, IsDoneRole }
This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.
As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?