首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道在QButtonGroup中按下了哪个按钮?

如何知道在QButtonGroup中按下了哪个按钮?
EN

Stack Overflow用户
提问于 2021-08-02 20:42:32
回答 2查看 1K关注 0票数 1

我有一个函数来创建一个框架,并在其中放置一个行编辑和一个按钮,然后将该按钮放在QButtonGroup上:

代码语言:javascript
复制
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

之后,我将连接到按钮组,以初始化remove函数,该函数将删除包含行编辑和按钮的整个框架。

代码语言:javascript
复制
self.btn_group.buttonClicked.connect(self.remove)

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-02 20:48:23

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

代码语言:javascript
复制
def remove(self, button):
    print(button, button.text())

演示:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-08-03 00:12:14

如果要在将按钮添加到组中时使用id对按钮进行编号,则可以使用idClicked(int id)信号。在某些用例中,使用id号比使用按钮名更容易。

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

代码语言:javascript
复制
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_())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68627740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档