Results 1 to 3 of 3

Thread: How to find which button was pressed (PyQt5)

  1. #1
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to find which button was pressed (PyQt5)

    Hello fellow qt users.

    In the process of learning python I created a tictactoe game and use the PyQt5 to create a gui. Simple 3x3 grid of buttons created in a loop with a view to a 4x4 grid later. Ran into a problem, how to identify the button pressed? Code below shows ugly solution used. Putting clicked.connect statements in the loop fails as it seems the variables i and j are passed by reference so they all contain 2, 2 when the loop exits. Full code here: https://github.com/RickTee/tictacQt. I've probably missed something some were in the docs.

    Qt Code:
    1. GRID_SIZE = 3
    2. # Create our board of 9 buttons and put them in a grid
    3. for i in range(0, GRID_SIZE ):
    4. for j in range(0, GRID_SIZE ):
    5. self.button[i][j] = QPushButton()
    6. self.button[i][j].setMinimumSize(25, 25)
    7. self.button[i][j].setMaximumSize(25, 25)
    8. grid.addWidget(self.button[i][j], i, j)
    9.  
    10. vbox.addLayout(grid)
    11. # Add the warnings label
    12. vbox.addWidget(self.labelWarn)
    13. vbox.addStretch(0)
    14. hbox.addLayout(vbox)
    15. hbox.addStretch(0)
    16. self.setLayout(hbox)
    17.  
    18. self.button[0][0].clicked.connect(lambda: self.on_button_clicked(0, 0))
    19. self.button[0][1].clicked.connect(lambda: self.on_button_clicked(0, 1))
    20. self.button[0][2].clicked.connect(lambda: self.on_button_clicked(0, 2))
    21. self.button[1][0].clicked.connect(lambda: self.on_button_clicked(1, 0))
    22. self.button[1][1].clicked.connect(lambda: self.on_button_clicked(1, 1))
    23. self.button[1][2].clicked.connect(lambda: self.on_button_clicked(1, 2))
    24. self.button[2][0].clicked.connect(lambda: self.on_button_clicked(2, 0))
    25. self.button[2][1].clicked.connect(lambda: self.on_button_clicked(2, 1))
    26. self.button[2][2].clicked.connect(lambda: self.on_button_clicked(2, 2))
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,470
    Thanked 273 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find which button was pressed (PyQt5)

    I don't know how in Python but in C++ it is a method QObject::sender().

  3. The following user says thank you to Lesiok for this useful post:

    RickTee (27th January 2020)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,317
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to find which button was pressed (PyQt5)

    self.button[0][0].clicked.connect(lambda: self.on_button_clicked(0, 0))
    And I don't know if Python allows you to do tricks that C++ cannot, but in C++, the slot that is connected to a signal must have the same signature (usually) as the signal: the clicked() signal has no arguments, so the on_clicked() slot cannot have any either. Even when the slot is a lambda, the arguments will be stripped off when the slot is called, if it compiles at all.

    The exception to the rule is a signal that -does- pass arguments can be connected to a slot defined with fewer arguments (in which case the extra arguments are simply ignored).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    RickTee (29th January 2020)

Similar Threads

  1. Replies: 5
    Last Post: 25th January 2017, 10:47
  2. do something while a button is pressed
    By saman_artorious in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2013, 16:39
  3. Replies: 6
    Last Post: 4th October 2010, 04:19
  4. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 16:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.