QTableWidgetItem - type
-
Hi,
I am fiiling a
QTableWidget
with data while theQTableWidgetItem
s are generated by two different ways.self.ui.tableEntries.setItem(0, ABVF.BESCHREIBUNG, QTableWidgetItem("any text"))
and
witem = QTableWidgetItem(locale.format_string("%.2f", 3.14159, True, True)) witem.setTextAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight) self.ui.tableEntries.setItem(0, ABVF.AUSGABE, witem)
When I then want to access the items by another method
self.ui.tableEntries.item(0, ABVF.BESCHREIBUNG).data(Qt.ItemDataRole.DisplayRole) self.ui.tableEntries.item(0, ABVF.AUSGABE).data(Qt.ItemDataRole.DisplayRole)
I get errors.
For column
ABVF.BESCHREIBUNG
everything works fine, for columnABVF.AUSGABE
I getAttributeError: 'NoneType' object has no attribute 'data'
. This depends not on the column index, since I have more than one column set by each way. If theQTableWidgetItem
is created directly in thesetItem
-method is different as if created separately.Why is
QTableWidgetItem
for columnABVF.AUSGABE
aNoneType
?All data are shown correctly in the table! No column is missing, all cells are filled!
The constructor of
QTableWidgetItem
has a default parametertype:int
. What is meant by that parameter? Is this one ofQTabWidgetItem.ItemType
? Tryingwitem = QTableWidgetItem(locale.format_string("%.2f", 3.15149, True, True), QTableWidgetItem.ItemType.Type)
changes nothing.
--EDIT:--
it is coming from
locale.string_format
. I have still no clue why. -
OK, I found my mistake.
It is not because of
locale.string_format
. Two of the columns are to separated positive and negative values. One is holding all positives and the other all negativs.If a positive value is found I created a
QTableWidgetItem
for the 'positive' column but forgott the create one for the 'negative' column and vice versa.Stupid!
-
@MasterQ
You will get theNoneType
if there is indeed noQTableWidgetItem
at the specified row, column. Either of the two methods you show should work fine. Consequently please set up a standalone project and test both in (0, 0) and (0, 1) respectively. If that goes wrong please post your exact, minimal code. -
@MasterQ said in QTableWidgetItem - type:
it is coming from locale.string_format. I have still no clue why.
Start, as ever, by simplifying. Remove the
, True, True
. Try with a format simpler than%.2f
. Maybe setlocale.setlocale(locale.LC_ALL, '')
before the call. -
OK, I found my mistake.
It is not because of
locale.string_format
. Two of the columns are to separated positive and negative values. One is holding all positives and the other all negativs.If a positive value is found I created a
QTableWidgetItem
for the 'positive' column but forgott the create one for the 'negative' column and vice versa.Stupid!
-