QButtonGroup管理状态按钮

参考文档:​​https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QButtonGroup.html​

QButtonGroup的作用

QButtonGroup管理状态按钮

从文件的详细描述中可以获得以下信息:

[En]

The following information can be obtained from the detailed description of the document:

  • 这是一个抽象的容器,当中可以放到一些​ ​按钮小部件​
  • 它只管理按钮的状态,不负责显示。
    [En]

    it only manages the status of the button and is not responsible for displaying it.*

  • 通过​ ​exclusive​​​属性可以设置,​ ​QButtonGroup​​​是否只能有一个按钮为选中状态,默认为​ ​True​
  • 可以通过​ ​setId(btn, id)​​​函数为容器中的按钮关联一个整数,通过​ ​id(btn)​​获取关联的id.

  • ​checkButton()​​​或以获取被选中的按钮(只能返回一个,所以只能用于​ ​exclusive == True​​​的情况),​ ​checkedId​​获得选中按钮的id.

  • ​buttons​​函数可以返回容器器中的按钮。

QButtonGroup一般用于管理以下三种按钮:

  • ​QPushButton​​​它的属性​ ​checkable == Ture​​​时,即它的实例调用了​ ​setCheckable(True)​​函数
  • ​QRadioButton​
  • ​QCheckBox​

独占示例

from PySide6.QtWidgets import QApplication,\    QWidget, QVBoxLayout, QHBoxLayout,\    QRadioButton, QLabel, QButtonGroup, \    QPushButtonclass Window(QWidget):    def __init__(self):        super().__init__()        self.__set_up_ui()    def __set_up_ui(self):        v_box = QVBoxLayout(self)        v_box.addWidget(QLabel("你的职业是:"))        h_box = QHBoxLayout()        v_box.addLayout(h_box)        lst = [            QRadioButton("程序员"),            QRadioButton("程序媛"),            QRadioButton("程序猿"),        ]        qbg = QButtonGroup(self)        self.qgb = qbg        _id = 1        for qrb in lst:            h_box.addWidget(qrb)            qbg.addButton(qrb, _id)            _id += 1        btn = QPushButton("获取职业")        btn.clicked.connect(lambda: print(self.qgb.checkedId()))        v_box.addWidget(btn)app = QApplication([])w = Window()w.show()app.exec()

非独占式

调用以下函数以使其成为非独占的。

[En]

Call the following function to make it non-exclusive.

qbg.setExclusive(False)

相应的​ ​QRadioButton​​改为:

lst = [    QCheckBox("程序员"),     QCheckBox("程序媛"),    QCheckBox("程序猿"),]

为了能获取选中的项,我们需要遍历​ ​buttons()​​判断按钮状态

class CheckBoxGroup(QButtonGroup):    def checkedId(self) -> list[int]:        lst = []        for btn in self.buttons():            if btn.isChecked():                lst.append(self.id(btn))        return lst

​CheckBoxGroup重载了checkedId​

完整代码如下:

from PySide6.QtWidgets import QApplication, QWidget, \    QVBoxLayout, QHBoxLayout, QLabel, QButtonGroup, \    QPushButton, QCheckBoxclass CheckBoxGroup(QButtonGroup):    def checkedId(self) -> list[int]:        lst = []        for btn in self.buttons():            if btn.isChecked():                lst.append(self.id(btn))        return lstclass Window(QWidget):    def __init__(self):        super().__init__()        self.__set_up_ui()    def __set_up_ui(self):        v_box = QVBoxLayout(self)        v_box.addWidget(QLabel("你的职业是:"))        h_box = QHBoxLayout()        v_box.addLayout(h_box)        lst = [            QCheckBox("程序员"),            QCheckBox("程序媛"),            QCheckBox("程序猿"),        ]        qbg = CheckBoxGroup(self)        qbg.setExclusive(False)        self.qgb = qbg        _id = 1        for qrb in lst:            h_box.addWidget(qrb)            qbg.addButton(qrb, _id)            _id += 1                                btn = QPushButton("获取职业")        btn.clicked.connect(lambda: print(self.qgb.checkedId()))        v_box.addWidget(btn)app = QApplication([])w = Window()w.show()app.exec()

Original: https://blog.51cto.com/u_12072082/5574425
Author: luan_225
Title: QButtonGroup管理状态按钮

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500809/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球