How to know which button was pressed inside a QButtonGroup?

2.8k Views Asked by At

I have a function that create a frame and place a line edit and a button inside of it, then it places the button on QButtonGroup:

self.btn_group = QButtonGroup(self.ui_main.scrollArea_content)

def new(self):
    # Create the frame
    self.ui_main.frame_content = QFrame(self.ui_main.scrollArea_content)
    self.ui_main.frame_content.setObjectName(f"frame_content_{self.counter}")
    self.ui_main.frame_content.setMaximumSize(QSize(16777215, 70))
    self.ui_main.frame_content.setFrameShape(QFrame.StyledPanel)
    self.ui_main.frame_content.setFrameShadow(QFrame.Raised)
    # Create horizontal layout
    self.ui_main.horizontalLayout_4 = QHBoxLayout(self.ui_main.frame_content)
    self.ui_main.horizontalLayout_4.setObjectName(f"horizontalLayout_4_{self.counter}")
    # Create line edit content
    self.ui_main.line_content = QLineEdit(self.ui_main.frame_content)
    self.ui_main.line_content.setObjectName(f"line_content_{self.counter}")
    # Add line edit content to horizontal layout
    self.ui_main.horizontalLayout_4.addWidget(self.ui_main.line_content)
    # Create the button remove
    self.ui_main.btn_remove = QPushButton(self.ui_main.frame_content)
    self.ui_main.btn_remove.setObjectName(f"btn_remove_{self.counter}")
    self.ui_main.btn_remove.setMinimumSize(QSize(0, 50))
    self.ui_main.btn_remove.setMaximumSize(QSize(80, 16777215))
    self.ui_main.btn_remove.setText(u"Remove")
    # Add button remove to horizontal layout
    self.ui_main.horizontalLayout_4.addWidget(self.ui_main.btn_remove)
    # Add the frame content(contains line edit and button) to vertical layout
    self.ui_main.verticalLayout_3.addWidget(self.ui_main.frame_content, 0, Qt.AlignTop)
    # Add button to group
    self.btn_group.addButton(self.ui_main.btn_remove)
    # Increment the counter to name the next button
    self.counter += 1

After that i have the connection to the button group to initialize the remove function, that will remove the entire frame containing the line edit and the button

self.btn_group.buttonClicked.connect(self.remove)

But with button group i was able to know only if any button was clicked, not which button was clicked, the buttonClicked method returns a button group object that contains nothing about the clicked button, there is a way to know which button was pressed inside QButtonGroup? if not how could i solve this?

2

There are 2 best solutions below

1
On BEST ANSWER

The buttonClicked signal sends the button that was clicked:

def remove(self, button):
    print(button, button.text())

Demo:

from PyQt5 import QtWidgets


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    view = QtWidgets.QWidget()
    lay = QtWidgets.QVBoxLayout(view)
    group = QtWidgets.QButtonGroup()
    for i in range(5):
        button = QtWidgets.QPushButton(f"button-{i}")
        lay.addWidget(button)
        group.addButton(button)
    view.show()

    def handle_button_clicked(button):
        print(button, button.text())

    group.buttonClicked.connect(handle_button_clicked)

    sys.exit(app.exec_())
1
On

If you want to number your buttons with id as you add them to the group, you can use the idClicked(int id) signal. There are some use cases where it is easier to work with id numbers than with button names.

Example (heavily borrowed from @eyllanesc excellent answer):

from PyQt5 import QtWidgets


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    view = QtWidgets.QWidget()
    lay = QtWidgets.QVBoxLayout(view)
    group = QtWidgets.QButtonGroup()
    for i in range(5):
        button = QtWidgets.QPushButton(f"button-{i}")
        lay.addWidget(button)
        group.addButton(button, i)
    view.show()

    def handle_button_idclicked(id_):
        print('id of button is ', id_)
        print('text of button is ', group.button(id_).text())

    group.idClicked.connect(handle_button_idclicked)

    sys.exit(app.exec_())