Connect(self.method) does not emit a signal, but (lambda: self.method()) does?



  • Suddenly my QPushButtons (which used to work perfectly) have decided not to emit signals (either that or their signals are not connecting properly?). I can't figure out what I changed which caused it to "break."

    I have found that the buttons do emit a signal when I use: button.clicked.connect(lambda: self.method()) -- but when I use button.clicked(self.method) there is no response. (Unfortunately using the lambda version breaks a lot of my remaining code which relies on self.sender()).

    Here is how this portion of my script is structured:

    My Central Widget has a grid layout (self.grid). I populate the grid with several rows of (dynamically generated) buttons & widgets. Each row is its own instance of my class EntryRow(QWidget).

    Here is the relevant method of my Central Widget:

    @
    def populateMainContent(self):

        self.getPageSettings()
        for index, thingy in enumerate(self.dummyData.dataDict[self.selectedThingies]):
            rowNum = index
            
     newRow = EntryRow(thingy, self.dummyData, self.grid, rowNum)
    
            mainWidgets = newRow.createMainWidgets()
            pageWidgets = newRow.createPageWidgets(self.selectedWidgets)
    
            widgetsToShow = mainWidgets + pageWidgets
    
            newRow.displayWidgets(widgetsToShow)@
    

    And here is my class EntryRow(QWidget) :

    @
    class EntryRow(QWidget):
    def init(self, name, dummyData, grid, rowNum):
    super().init()

        self.name = name
        self.widgetList = {"Main" : [],
                           "theseWidgets" : [],
                           "noIMeanThoseWidgets" : []
                           }
        self.grid = grid
        self.rowNum = rowNum
        self.dummyData = dummyData
    
    def createMainWidgets(self):
        self.nameLabel = QLabel(self.name)
        self.widgetList["Main"].append(self.nameLabel)
        return self.widgetList["Main"]
    
    def createPageWidgets(self, selectedWidgets):
        for widName in self.dummyData.widgetDict[selectedWidgets]:
            if "Button" in widName:
                newWidget = QPushButton(widName)
    

    newWidget.setCheckable(True)
    elif "Check" in widName:
    newWidget = QCheckBox(widName)

     ###########################################################
            newWidget.clicked.connect(self.method)  ## DOES NOT SEEM TO EMIT A SIGNAL
     ##########################################################
     #newWidget.clicked.connect(lambda: self.method())  # THIS DOES EMIT A SIGNAL
     ##########################################################
    
            self.widgetList[selectedWidgets].append(newWidget)
        return self.widgetList[selectedWidgets]
    
    def displayWidgets(self, widgetsToShow):
        for index, wid in enumerate(widgetsToShow):
            colNum = index
            self.grid.addWidget(wid, self.rowNum, colNum )
    
    def method(self):
        print("Signal received")
    

    @

    Any help would be appreciated. I assume it's a problem of parentage or layout?

    Thanks



  • Try decorating the slot as follows:

    @
    ...
    @QtCore.Slot
    def method(self):
    print("Signal received")
    ...
    @

    Hope this helps ;o)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.