9

I want to associate additional data with each QTableWidgetItem inserted into the table, in order to use that data in future, when it is being clicked on a table item. But that data should not be visible. How can I do that?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • 2
    This is one of my biggest complaints against Qt. I've yet to find, in the few months I've been using it, a simple mechanism analogous to data binding in .NET. – San Jacinto Apr 05 '10 at 16:53
  • All of Qt's item display classes (trees,tables,lists) are based on Model/View, it's easy to make the backend model anything you like. There isn't the one click link to SQL you have with LINQ but it's more flexible. – Martin Beckett Apr 06 '10 at 14:44
  • 2
    @Martin I call it "feasible," not "easy"... although I guess it's better than having the data tied directly to the presentation. – San Jacinto Apr 08 '10 at 20:07
  • @San Jacinto - perhaps 'sensible' would be a better word! ;-) – Martin Beckett Apr 08 '10 at 22:20

2 Answers2

23

You can use QTableWidgetItem::setData() like so:

setData(Qt::UserRole, myData); // set

Where myData is a supported QVariant type. You can use QTableWidgetItem::data() to retrieve the value that you store.

If you need more than one you can use Qt::UserRole + 1, + 2, and so on (Qt::UserRole is "The first role that can be used for application-specific purposes.", you can read more about the other types of roles here).

If you're storing a custom type that isn't natively supported by QVariant you will need to register your type with the Qt meta-object system. Look at QMetaType for more details on that.

If you wanted to store an integer, for example:

QTableWidgetItem* widgetItem = tableWidget->item(row, col); // get the item at row, col
int myInteger = 42;
widgetItem->setData(Qt::UserRole, myInteger);
// ...
myInteger = widgetItem->data(Qt::UserRole);
Drise
  • 4,310
  • 5
  • 41
  • 66
richardwb
  • 4,339
  • 33
  • 19
  • How can I associate an integer to table item with setData() function? Should I do following: item.setData(Qt::UserRole, myInteger)? – Narek Apr 05 '10 at 17:02
  • Thank you very much! I have used this function bu with other Qt Roles and effect was not what I want so I got confused! Thanks! – Narek Apr 05 '10 at 17:07
  • 3
    If you are adding more than 2 data elements to each table item, it starts to get easier to create a model and use QTableView instead, IMO. – Caleb Huitt - cjhuitt Apr 06 '10 at 15:06
6

You could derive from QTableItem and provide your own data member, or you could use the QTableView with your own model.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    From my experience this is much more flexible than setData data Qt functions – drahnr Apr 06 '10 at 06:14
  • 1
    Yes but richard's answer is probably easier for a beginner - or if you are just trying to translate some MFC code – Martin Beckett Apr 06 '10 at 14:55
  • For those reading in the future, QTableItem is now [QTableWidgetItem](https://doc.qt.io/archives/qt-4.8/qtablewidgetitem.html), and supports subclassing still. – LolPython Feb 18 '20 at 18:44