0

when I try to write to a specific cell in an QTablewidget I only got an TypeError, although I follow the documentation.

The documentation instructs me:

self.tblLieferboxen.setItem(0,0,'foo')

which leads to TypeError: setItem(self, int, int, QTableWidgetItem): argument 3 has unexpected type 'str'

Then I assumed that it wanted 'self' passed as the first parameter and did so with

self.tblLieferboxen.setItem(self, 0,0,'foo')

but again TypeError: setItem(self, int, int, QTableWidgetItem): argument 1 has unexpected type 'deckblaetterUi'.

Then I thought I would be clever and pass the parameters as keyword-args ...

self.tblLieferboxen.setItem(row=0, column=0, item='foo')

but it wasn't meant to be ...TypeError: setItem() takes no keyword arguments

What am I doing wrong or what have I overlooked to write to a single cell (not a whole row)?

The complete class:

from PyQt5 import QtWidgets, uic, QtCore, QtGui

class deckblaetterUi(QtWidgets.QMainWindow):
    def __init__(self):
        super(deckblaetterUi, self).__init__()
        uic.loadUi('ui/deckblaetter.ui', self)

        self.rb3040.clicked.connect(self.waehle3040)
        self.rb3030.clicked.connect(self.waehle3030)
        self.rb2121.clicked.connect(self.waehle2121)
        self.rb2121C.clicked.connect(self.waehle2121C)

        self.tblLieferboxen.setItem(0, 0, 'foo')

        self.btnBoxenlabelsErstellen.clicked.connect(self.btnBoxenlabelsErstellenClicked)

        self.show()
hirnwunde
  • 383
  • 3
  • 5
  • The function takes three parameters: 2 ints and a QTableWidgetItem. You seem to be passing a string instead of an QTableWidgetItem object. You should try creating such an object first, and then passing as the third argument to .setItem – Peter Aug 23 '22 at 14:01
  • Thank you Peter ... those who can read have a clear advantage. Obviously I do not belong to this species. – hirnwunde Aug 23 '22 at 14:54

1 Answers1

0

Thank you Peter.

Same I read here (before i read your comment).

I did not read the documentation correctly and wrongly assumed that item was a string. But it is clearly stated there that item must be of type PySide2.QtWidgets.QTableWidgetItem

With the following code everything runs as it should:

self.tblLieferboxen.setItem(0, 0, QtWidgets.QTableWidgetItem('foo'))
hirnwunde
  • 383
  • 3
  • 5