Important: Please read the Qt Code of Conduct -

Bugs when getting objectName QPushButton

  • Hi!
    In the loop, I create a QPushButton and add it to gridLayout.
    When trying to get objectName, it always returns the objectName of the last button. How do I get the objectName of the desired button?

                buttonOpen = QPushButton(self.ui)
                # lambdaButtonOpen = lambda: self.openChartOnId(int(buttonOpen.objectName()))
                # lambdaButtonOpen = lambda: print(buttonOpen.objectName())
                z = int(i)
                lambdaButtonOpen = lambda : print(z)
                self.lastIndexX = 0
                self.lastIndexY += 1
                self.ui.gridLayoutPage0.addWidget(buttonOpen, self.lastIndexY, self.lastIndexX)

  • @Mikeeeeee
    It's difficult to be sure without seeing your whole code. But I believe what you are seeing is that

    z = int(i)
    lambdaButtonOpen = lambda : print(z)

    means that lambda always prints the value of z as it was last in the function, hence the behaviour you see.

    You need to pass the desired value of i or z as a parameter to the lambda:

    z = int(i)
    lambdaButtonOpen = lambda z : print(z)

    or, without any z:

    lambdaButtonOpen = lambda i : print(int(i))

    Now if should work.

    Note, however, that Python discourages the use of setting a variable/label (lambdaButtonOpen) to a lambda.

  • @JonB But how do I find out the index of the button when I click it? I always get the same number, there is a link to the pointer. How do I pass a value instead of a pointer?

  • @Mikeeeeee said in Bugs when getting objectName QPushButton:

    I always get the same number,

    I don't understand. I have suggested why you always get the same number printed out in your code, and what you must do to that code to get the correct number. Have you changed your code as I wrote or have you ignored it? Does my change cause a different number to be printed out from what you had, yes or no?

  • @JonB said in Bugs when getting objectName QPushButton:

    lambdaButtonOpen = lambda z : print(z)
    You can't write this way, because the signal doesn't have any additional values and you will get an False.

    lambdaButtonOpen = lambda z : print(z)

    it is work, but returns the last element of the while loop

    z = int(i)
    lambdaButtonOpen = lambda : print(z)

  • @Mikeeeeee
    You don't show any while loop. Leaving me to guess, I will assume you are incrementing i, though it would be better if you showed the code.

    it is work, but returns the last element of the while loop

    You state this, with one piece of code above it and one piece below it. Which does your comment about "not working" apply to?

    If it is:

    z = int(i)
    lambdaButtonOpen = lambda : print(z)

    This is your original code. I pasted what to change that to if you still wish to use z variable.

    If you have tried what I wrote for the two possibilities, and neither of them works, then try removing the variable (lambdaButtonOpen) assigned to a lambda, and replace with:

    buttonOpen.clicked.connect(lambda i : print(int(i)))

  • I redefined QPushButton and passed objectName with the signal

    class MyPushButton(QPushButton):
        myClicked = pyqtSignal(str)
        def slotForMySignal(self):
        def __init__(self, *args):

Log in to reply