Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Bugs when getting objectName QPushButton

Bugs when getting objectName QPushButton

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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)
                buttonOpen.setText("Text")
                buttonOpen.setObjectName(str(i))
                # lambdaButtonOpen = lambda: self.openChartOnId(int(buttonOpen.objectName()))
                # lambdaButtonOpen = lambda: print(buttonOpen.objectName())
                print(buttonOpen.objectName())
                z = int(i)
                lambdaButtonOpen = lambda : print(z)
                buttonOpen.clicked.connect(lambdaButtonOpen)
                self.widgetsPage0.append(buttonOpen)
    
                self.lastIndexX = 0
                self.lastIndexY += 1
                self.ui.gridLayoutPage0.addWidget(buttonOpen, self.lastIndexY, self.lastIndexX)
    
    JonBJ 1 Reply Last reply
    0
    • M Mikeeeeee

      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)
                  buttonOpen.setText("Text")
                  buttonOpen.setObjectName(str(i))
                  # lambdaButtonOpen = lambda: self.openChartOnId(int(buttonOpen.objectName()))
                  # lambdaButtonOpen = lambda: print(buttonOpen.objectName())
                  print(buttonOpen.objectName())
                  z = int(i)
                  lambdaButtonOpen = lambda : print(z)
                  buttonOpen.clicked.connect(lambdaButtonOpen)
                  self.widgetsPage0.append(buttonOpen)
      
                  self.lastIndexX = 0
                  self.lastIndexY += 1
                  self.ui.gridLayoutPage0.addWidget(buttonOpen, self.lastIndexY, self.lastIndexX)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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.

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @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.

        M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        @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?

        JonBJ 1 Reply Last reply
        0
        • M Mikeeeeee

          @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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            @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)
            
            JonBJ 1 Reply Last reply
            0
            • M Mikeeeeee

              @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)
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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)))
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                I redefined QPushButton and passed objectName with the signal

                class MyPushButton(QPushButton):
                    myClicked = pyqtSignal(str)
                
                    def slotForMySignal(self):
                        self.myClicked.emit(self.objectName())
                
                    def __init__(self, *args):
                        super().__init__()
                        self.clicked.connect(self.slotForMySignal)
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved