What is the role in QTreeWidgetItem?

I have a QTreeWidget with multiple columns, I add QTreeWidgetItems to it. I am trying to have the second column contain a numeric value for each Item , so I can sort the items by this value

 QTreeWidgetItem has a method called setData(int column, int role, QVariant(data)) 

I cannot find documentation for this role argument. All I know is that if I set it to 1 or 2, something will appear in the column, if I set it to 0 or> = 3, nothing is displayed in the column, regardless of whether the numbers always end sorted alphabetically, which is incorrect.

+7
model-view-controller qt pyqt qabstractitemmodel
source share
3 answers

You can use Qt :: UserRole for specific application purposes. Since this data is QVariant, you can create a QList to set multiple data and then pass it to QVariant and set the data.

Here is an example:

 QTreeWidgetItem* item = new QTreeWidgetItem(); QList<QVariant> dataList; dataList.append("data 1"); dataList.append("data 2"); QVariant data(dataList); item->setData(0, Qt::UserRole, data); 
+8
source share

The relevant documentation can be found in the Qt :: ItemDataRole section (found through QAbstractItemModel :: InstallData ). Roles are used to indicate for which data the data should be used. You can use different roles to set the tooltip, font, or color of an element, among other things.

+6
source share

Note that item->text() is a convenience equivalent to item->data(Qt::DisplayRole).toString()

+4
source share

All Articles