I tried to use the cities-standarditem of the Qt exemple and adapt it to my exemple. I have a strange result:
Here is my User class:
class User{
public:
User();
QString getFirstname() const;
void setFirstname(const QString &value);
QString getLastname() const;
void setLastname(const QString &value);
int getAge() const;
void setAge(int value);
private:
QString firstname;
QString lastname;
int age;
};
and i have declared a usermodel.h:
class UserModel: public QStandardItemModel
{
Q_OBJECT
public:
UserModel(QList<User> users, QObject *parent = Q_NULLPTR);
QHash<int, QByteArray> roleNames() const;
};
And here is the implementations of the constructor and the roleNames functions:
enum ItemRoles {
FirstnameRole,
LastnameRole,
AgeRole,
};
UserModel::UserModel(QList<User> users, QObject *parent) : QStandardItemModel(parent)
{
//this->setItemRoleNames(roleNames());
this->setColumnCount(3);
for (auto user: users) {
QStandardItem *item = new QStandardItem();
item->setData(user.getFirstname(), FirstnameRole);
item->setData(user.getLastname(), LastnameRole);
item->setData(user.getAge(), AgeRole);
appendRow(item);
}
setSortRole(FirstnameRole);
}
QHash<int, QByteArray> UserModel::roleNames() const
{
QHash<int, QByteArray> mapping = QStandardItemModel::roleNames();
mapping[FirstnameRole] = "firstname";
mapping[LastnameRole] = "lastname";
mapping[AgeRole] = "age";
return mapping;
}
My table view only show the last Role added with the function:
item->setData(user.getFirstname(), FirstnameRole);
If its the age lastly added, its the age showed... Any clues ?
Let's say you really need a custom model and want to extend an existing one. Since your data is in tabular fashion, I'd suggest to use
QAbstractTableModelas the base class.So let's have this class:
As you can see, the class itself stores a list of users, given at construction time. The constructor itself does nothing but copy-initializing the list.
Then you need to supply these implementation, at least:
While
columnCountis constant and always returns 3,rowCountwill return the number of items in the list of users. Indataimplementation, the index passed is inspected and a value is returned, according to the index row and column, and the passed role. It's important to understand that when the view callsdatapassing aroleequal toQt::DisplayRole, the function should return the very data that will be shown in the cell at(index.row(), index.column()), in our case: one of the threeUser's data members.It's quite useful to reimplement the
sortfunction as well, i.e.This way you let the user sort by column, as expected:
If, for some reason, you want to use custom roles, please have your enum be like this:
Starting from
Qt::UserRole(which is there for this very purpose) ensures that your roles don't clash whit builtin roles.Please notice that the above code is meant to suggest a possible solution and it's not the solution itself (and lacks many important features like bounds checking and memory management).