Issue
I’m a beginner with QT Python and I’m trying to run a small program to display a table view with a push button beside each row. I’ve tried the code shown below but I’m getting the following errors:
Traceback (most recent call last):
File "/Users/Programs/bottone.py", line 42, in <module>
mainwindow = MainWindow()
File "/Users/Programs/bottone.py", line 29, in __init__
self.btn_sell = IndexedButtonWidget('Edit')
File "/Users/Programs/bottone.py", line 8, in __init__
super(QPushButton, self).__init__(parent=parent)
TypeError: arguments did not match any overloaded call:
QPushButton(parent: QWidget = None): argument 'parent' has unexpected type 'str'
QPushButton(str, parent: QWidget = None): not enough arguments
QPushButton(QIcon, str, parent: QWidget = None): not enough arguments
Code:
class IndexedButtonWidget(QPushButton):
def __init__(self, parent=None):
super(QPushButton, self).__init__(parent=parent)
self.button_row = 0
self.button_column = 0
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self,parent)
self.table = QtWidgets.QTableWidget()
self.table.setColumnCount(3)
self.setCentralWidget(self.table)
data1 = ['row1','row2','row3','row4']
data2 = ['1','2.0','3.00000001','3.9999999']
self.table.setRowCount(4)
for index in range(4):
item1 = QtWidgets.QTableWidgetItem(data1[index])
self.table.setItem(index,0,item1)
item2 = QtWidgets.QTableWidgetItem(data2[index])
self.table.setItem(index,1,item2)
self.btn_sell = IndexedButtonWidget('Edit')
self.btn_sell.button_row = index
self.btn_sell.button_column = 2
self.btn_sell.clicked.connect(self.handleButtonClicked)
self.table.setCellWidget(index,2,self.btn_sell)
def handleButtonClicked(self):
button = self.sender()
print(button.button_row)
print(button.button_column)
Do you have any clue about which is the problem?
Solution
The IndexedButtonWidget
is instantiated with one argument: 'Edit'
self.btn_sell = IndexedButtonWidget('Edit')
The documentation shows that there is only one QPushButton.__init__()
which takes one argument: QPushButton(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None
. So the code ends up passing 'Edit'
as parent
, hence the TypeError
.
help(QPushButton)
Help on class QPushButton in module PySide6.QtWidgets: class QPushButton(QAbstractButton) | QPushButton(self, icon: Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap], text: str, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | QPushButton(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | QPushButton(self, text: str, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | | Method resolution order: | QPushButton | QAbstractButton | QWidget | PySide6.QtCore.QObject | PySide6.QtGui.QPaintDevice | Shiboken.Object | builtins.object | | Methods defined here: | | __delattr__(self, name, /) | Implement delattr(self, name). | | __init__(...) | __init__(self, icon: Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap], text: str, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | __init__(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | __init__(self, text: str, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None | | Initialize self. See help(type(self)) for accurate signature.
This error can be solved by updating IndexedButtonWidget.__init__()
.
class IndexedButtonWidget(QPushButton):
def __init__(self, text='', parent=None):
super(QPushButton, self).__init__(text, parent=parent)
self.button_row = 0
self.button_column = 0
There are other ways to write __init__()
so that the TypeError
doesn’t occur, but this solves the specific error in the question.
Answered By – Michael Ruth
Answer Checked By – Marilyn (BugsFixing Volunteer)