Reputation: 1975
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?
Upvotes: 3
Views: 1236
Reputation: 736
In the last line of your code, in the connect
method, I believe you have a typo in your code
self.connect(self.mapper, QtCore.SIGNAL("mapped(int)"), self.removeItem(),
QtCore.SIGNAL("clicked(int)"))
should be
self.connect(self.mapper, QtCore.SIGNAL("mapped(int)"), self.removeItem,
QtCore.SIGNAL("clicked(int)"))
Having self.removeItem()
in the connect method will actually try to call the self.removeItem method
rather than providing the subsystem an address to connect the function
As finmor suggests, you should look at new syntax signals and slots as they will dramatically help to clarify your code and make it more Pythonic.
Upvotes: 3
Reputation: 451
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)
Upvotes: 3