Skip to content
Advertisement

Cannot populate QTableWidget using .setItem(row, colum, QTableWidgetItem(data))

I wanted to test PyQt to write a quick app to display and edit data in an Excel like form but the data is never shown.

Both the docs and the book I read say that using .setItem(row, colum, QTableWidgetItem(data)) on a QtableWidget object is one way to go. However, the following code doesn’t work, I only have an empty table and I can’t figure out why. Any idea ?

JavaScript

This is what the window looks like when I run the code

Advertisement

Answer

If you want to display the data in a QTableWidgetItem and pass it through the constructor then it must be a string.

JavaScript

The downside is that it is no longer a number but a string that represents a number.

Another better option is to use setData to pass the number to the Qt.DisplayRole role.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement