I am trying to insert and display integers in my QTableWidget. They don't display. It works if I convert everything to strings, but then I can't sort columns numerically--only lexically (1, 10, 100, etc.). This is using PyQt.
I've tried some of the suggested solutions, using QTableWidgetItem.setData(someRole,intValue), bu then nothing at all displays. I've tried, Qt.UserRole, DisplayRole and Edit Role. (I don't understand why these Roles are needed to display integers, but have just followed the examples). My specific code is:
item = QTableWidgetItem()
item.setData = (Qt.DisplayRole,intValue)
myTable.setItem(row, column, item)
The following code works, but for strings only:
item = QTableWidgetItem(str(intValue))
myTable.setItem(row, column, item)
Also, the suggestions for reading the data back, only show the object location, not the actual data. Example, using Eric as an interpreter shell:
item.data(Qt.DisplayRole)
Response: PyQt4.QtCore.QVariant object at 0x1f01fa60
or this:
item.data(Qt.EditRole).data()
Response: sip.voidptr object at 0x1e904a80
Any insight is appreciated.
You were on the right track. Your code doesn't work because you're not calling the
QTableWidgetItem
'ssetData()
function but trying to assign it a value. You haveinstead of
Also, when reading the data back it's not just the location that's shown but the data itself as a QVariant. You should find that
item.data(Qt.DisplayRole).toString()
will return your data back as a string by converting theQVariant
(via its.toString()
method).Here's a quick working example just to demonstrate: