1

I am looking through the documentation at:

I can put the line IDList.append(item.data()) into my code, and print that as the correct value. But weirdly, after that line, it gives me this error:

TypeError: QTableWidgetItem.data(int): not enough arguments

I don't know why the error message comes after the print line, but I don't think that should be important. What does the documentation mean by "int role"? Can you give an example, please?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Paul Jackways
  • 71
  • 1
  • 1
  • 11

1 Answers1

2

An item has many kinds of data associated with it, such as text, font, background, tooltip, etc. The role is a value from the ItemDataRole enum that allows you to specify which kind of data you want.

Some of these items of data also have an accessor function. So these two lines of code are equivalent:

font = item.font()
font = item.data(QtCore.Qt.FontRole)

The data roles are extensible. You can use any values starting at Qt.UserRole to associate a custom role with your own data:

MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, [1, 2, 3])
...
data = item.data(MyRole)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • How do I know what role to use? is there a list somewhere? – Paul Jackways Oct 13 '16 at 20:07
  • Oh, I see them in the link you added. Thank you. – Paul Jackways Oct 13 '16 at 20:16
  • I'm trying to get an Int from my item with data(). which role do I use for this? is there something like .number() that I can use in the item? – Paul Jackways Oct 17 '16 at 22:45
  • @PaulJackways. Are you using Python2? If you are, `data()` will return a `QVariant`, so you can use `.toInt()` to get a Python `int`. However, a much better solution is to follow the advice in [this answer](http://stackoverflow.com/a/21223060/984421), which will get rid of `QVariant` altogether. You can also do the same for `QString`, so you don't have to keep converting with `str` all the time. – ekhumoro Oct 17 '16 at 23:01
  • I'm using python 3.4, but I will try that anyway. – Paul Jackways Oct 18 '16 at 00:47
  • I tried adding `import sip sip.setapi('QVariant', 2)` and going `IDList.append(item[0].data(QtCore.Qt.DisplayRole))` and `IDList.append(item[0].data())`, neither of which worked. – Paul Jackways Oct 18 '16 at 01:00
  • @PaulJackways. In that case, please ignore my previous comment. I don't quite understand the problem you have, though. How did you set the data? – ekhumoro Oct 18 '16 at 01:01
  • basically what I am trying to do is get the value of the QTableWidgetItem. It is an integer, and I am trying to get it out of a QTableWidgetItem in a list of QTableWidgetItems I want to do exactly what QTableWidgetItem.text() does but for an integer. – Paul Jackways Oct 18 '16 at 01:03
  • There's no special method for this: just do `int(item.text())` or `float(item.text())`. – ekhumoro Oct 18 '16 at 01:06
  • I am trying that, but I am getting this: `ValueError: invalid literal for int() with base 10: ''` when using `IDvar = item[0].text()`, `print(IDvar)` I have checked, and I am sure that the item is a QTableWidgetItem. – Paul Jackways Oct 18 '16 at 01:19
  • @PaulJackways. Yes, that is exactly what should happen if you try to convert an invalid string to an `int`. An empty string is not a valid integer. What did you expect to happen? – ekhumoro Oct 18 '16 at 01:22
  • it isn't meant to be an empty string. I am trying to get the data that is in the item that is displayed in my QTable. I tried this: `def log_change(self, item):`, `self.table.blockSignals(False)`, `print("item row")`, `print(item.row())`, `#self.changed_items.append(self.table.item(item.row(),1)`, `print(self.table.item(item.row(),1).text())` The print statement prints nothing. – Paul Jackways Oct 18 '16 at 01:40
  • @PaulJackways. None of this has anything to do with the original question here. Please start a new question, and show all the relevant code. – ekhumoro Oct 18 '16 at 01:59