Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect PySide to a signal using QSignalMapper and an instance method

I have a small app with a table. This table has some data and a button on each row. These buttons should allow the user to remove corresponding row data. I'm trying to implement it via the clicked button signal, but I need to pass the row number, so I tried using QSignalMapper, as shown in the excerpt below

btnRemoveItem = QPushButton()
btnRemoveItem.clicked.connect(self.removeItem)
self.mapper = QSignalMapper(self)
self.connect(btnRemoveItem, QtCore.SIGNAL("clicked()"), self.mapper,
        QtCore.SLOT("map()"))
self.mapper.setMapping(btnRemoveItem, nextRow)
self.connect(self.mapper, QtCore.SIGNAL("mapped(int)"), self.removeItem(),
        QtCore.SIGNAL("clicked(int)"))

Problem is, my removeItem(self, index) method is an instance method (because my table belongs to a specific class) and I'm having trouble mapping it in a way I can pass self along with index.

Currently, my code fails with the following error:

TypeError: removeItem() takes exactly 2 arguments (1 given)

Is there a way to make this work correctly? Or is it impossible to map instance methods with QSignalMapper in PySide?

like image 900
Mauren Avatar asked Nov 22 '25 18:11

Mauren


1 Answers

I tried to reproduce your code in PyQt but I'm not fully aware of the differences between Pyside and PyQt so my answer is more of a guess. Try to remove the second line of your code and replace the last one with:

self.mapper.mapped.connect(self.removeItem)
like image 180
finmor Avatar answered Nov 24 '25 07:11

finmor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!