Search code examples
qtrowmetadataqtableviewuser-roles

QTableview row metadata


In a QTableView I'm trying to add some metadata for each row. An obvious solution would be to use the Qt::UserRole on the last column. Something like this.

QStandardItemModel* model = new QStandardItemModel();
QStandardItem* item = new QStandardItem();
item->setData("Hello", Qt::DisplayRole);
item->setData(rowMetadata, Qt::UserRole);
model->setItem(ROW_0, COL_0, item);
// Getting the metadata as follows
model->data(model->index(ROW_0, COL_0), Qt::UserRole);

Something feels wrong though. Any more intuitive solutions ?


Solution

  • One possibility is to subclass QAbstractItemModel and build your own internal model and then you can store meta data any way you like. If QStandardItemModel otherwise works well for you, then this might be more work than just sticking with what you're already doing.

    The other possibility I can think of is to use your existing QStandardItemModel and a QSortFilterProxyModel. In the base model, you can store your meta data as extra columns and then in the proxy, just make sure those columns are always hidden from the view.