【问题标题】:How to know which button was pressed inside a QButtonGroup?如何知道 QButtonGroup 中按下了哪个按钮?
【发布时间】:2021-10-08 04:04:42
【问题描述】:

我有一个函数可以创建一个框架并在其中放置一个行编辑和一个按钮,然后将按钮放置在 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

之后我连接到按钮组以初始化删除功能,这将删除包含行编辑和按钮的整个框架

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

但是对于按钮组,我只能知道是否单击了任何按钮,而不是单击了哪个按钮,buttonClicked 方法返回一个按钮组对象,其中不包含有关单击的按钮的任何内容,有一种方法可以知道哪个按钮是在 QButtonGroup 内按下?如果不是,我该如何解决?

【问题讨论】:

    标签: python pyqt pyside qbuttongroup


    【解决方案1】:

    buttonClicked 信号发送被点击的按钮:

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

    演示:

    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_())
    

    【讨论】:

    • 谢谢,这完全符合我的要求,问题出在我调用 self.sender() 并接收按钮组对象的删除函数中。
    【解决方案2】:

    如果您想在将按钮添加到组时使用id 编号,可以使用idClicked(int id) 信号。在某些用例中,使用 ID 号比使用按钮名称更容易。

    示例(大量借鉴@eyllanesc 的优秀答案):

    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_())
    

    【讨论】:

    • 感谢您的建议,使用 id 更容易。
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多