I\'m trying to use a (class derived from) QAbstractTableModel with a Qml TableView;
However, only the 1st column is displayed.
The reason is QVariant MyModel
The TableViewColumn API suggests that the data from the column is retrieved via roles instead of columns, i.e. "one", "two", and "three", while the column passed will be always 0. You return QVariant() for everything but Qt::DisplayRole. Qt::DisplayRole is 0, converted to int. In rolenames, you set the name for 0 to "one", so that's why you happen to see something for "one" (DisplayRole).
So to fix this, you must return something for one, two, and three. I'd suggest to define a custom roles enum in the header:
//In class MyModel:
enum Role {
OneRole=Qt::UserRole,
TwoRole,
ThreeRole
};
Define roleNames like this:
QHash<int, QByteArray> MyModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[OneRole] = "one";
roles[TwoRole] = "two";
roles[ThreeRole] = "three";
return roles;
}
Note that I started with Qt::UserRole instead of 0. That avoids collision with predefined roles such as Qt::DisplayRole.
Then return something for One, Two and Three, in data():
...
switch(role)
{
case OneRole:
return m_the_data[index.row()].m_one;
case TwoRole:
return m_the_data[index.row()].m_two;
case ThreeRole:
return m_the_data[index.row()].m_three;
}
...
Now you should see the data.
It seems that the TableView/TableViewColumn from QtQuickControls does a somewhat unfortunate and confusing mix of role and columns: While the naming let's one think of model columns (but they actually refer to the view's columns here), one can retrieve data only via different roles, with the column being fixed to 0. (To me there should be another optional property "column" in TableViewColumn.) It's a bit of a clash between the C++ QAbstractItemModel/QTableView way of things, where multiple columns are a natural thing to do, and QtQuick views, which all use only roles to refer to data, often not supporting multiple columns at all (ListView, GridView).