Arana
Arana

Reputation: 185

Converting from PyQt4 signal to PyQt5 signal doesn't work

I am trying to convert a game from PyQt4 to PyQt5 and I am having some difficulty converting to the newer signal types. So I have gone back into the original and I am trying to convert the signal first using PyQt4. The commented line is the old (working) version, underneath it the new version. However the 'new' version is always passing the same value to the event handler: 0. I have tried many subtly different versions, nothing seems to work. Ideas?

    self.cellButtonNames = [self.ui.cell0Button,  self.ui.cell1Button,  self.ui.cell2Button,  self.ui.cell3Button,  self.ui.cell4Button,  self.ui.cell5Button,
                                        self.ui.cell6Button, self.ui.cell7Button,  self.ui.cell8Button,  self.ui.cell9Button,  self.ui.cell10Button,  self.ui.cell11Button, 
                                        self.ui.cell12Button,  self.ui.cell13Button,  self.ui.cell14Button,  self.ui.cell15Button,  self.ui.cell16Button,  self.ui.cell17Button, 
                                        ...,
                                        self.ui.cell126Button,  self.ui.cell127Button,  self.ui.cell128Button,  self.ui.cell129Button,  self.ui.cell130Button,  self.ui.cell131Button,
                                        self.ui.cell132Button,  self.ui.cell133Button,  self.ui.cell134Button,  self.ui.cell135Button,  self.ui.cell136Button,  self.ui.cell137Button,
                                        self.ui.cell138Button,  self.ui.cell139Button,  self.ui.cell140Button,  self.ui.cell141Button,  self.ui.cell142Button,  self.ui.cell143Button]

    # This will connect all of the ball buttons to one slot (Event Handler) and pass the cell number to that event handler when signalled.
    # This elimates the need for 144 event handling methods! :-)

    cellNumber = 0
    for buttonName in self.cellButtonNames:
        # self.connect(buttonName, QtCore.SIGNAL("clicked()"), lambda who=cellNumber: self.ballClicked(who))
        buttonName.clicked.connect(lambda who=cellNumber: self.ballClicked(who))
        cellNumber += 1

@QtCore.pyqtSignature("")                                                                           # otherefore only want ne signal for this autoconnect.
def ballClicked ( self, cellNumber):
    print("Ball %i clicked" % cellNumber)

Upvotes: 2

Views: 728

Answers (1)

ekhumoro
ekhumoro

Reputation: 120568

This is quite a well-known "gotcha".

There are a few signals (like clicked and triggered) that have default parameter that always sends a value. For buttons and actions, this is a bool representing their current checked state (even though they may not have been set checkable).

To work around this, you can connect your signals like this:

    buttonName.clicked.connect(
        lambda checked, who=cellNumber: self.ballClicked(who))

However, in this particular case, you might also want to consider using a QButtonGroup instead. That would only require one signal connection, and, as a side-benefit, you'd also get a ready-made interface for managing the buttons.

Upvotes: 2

Related Questions