From a942e5e9311843fc77ac20ab69df4b744839ba99 Mon Sep 17 00:00:00 2001 From: muzing Date: Thu, 23 Jun 2022 08:50:33 +0800 Subject: [PATCH] Add 03-02-01-05 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QAbstractButton 按钮的排他性 --- .../05-QAbstractButton-排他性.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 03-QtWidgets-常用控件/02-Button-按钮控件/01-QAbstractButton-按钮类的抽象基类/05-QAbstractButton-排他性.py diff --git a/03-QtWidgets-常用控件/02-Button-按钮控件/01-QAbstractButton-按钮类的抽象基类/05-QAbstractButton-排他性.py b/03-QtWidgets-常用控件/02-Button-按钮控件/01-QAbstractButton-按钮类的抽象基类/05-QAbstractButton-排他性.py new file mode 100644 index 0000000..6350fb9 --- /dev/null +++ b/03-QtWidgets-常用控件/02-Button-按钮控件/01-QAbstractButton-按钮类的抽象基类/05-QAbstractButton-排他性.py @@ -0,0 +1,47 @@ +import sys + +from PySide6 import QtWidgets + +""" +QAbstractButton 自动排他功能 +该功能主要用于单选按钮:一组单选按钮都有排他性,即一次只能选中其中一项,选中新的项会导致之前选择的项取消 +除QRadioButton子类外,该功能默认关闭 +更多信息参见本项目 QRadioButton 目录 + +.setAutoExclusive(bool) 设置是否开启自动排他性,默认为false +.autoExclusive() -> bool 获取按钮是否开启了自动排他性 +""" + + +class MyWidget(QtWidgets.QWidget): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.setWindowTitle("QAbstractButton-排他性") + self.resize(800, 600) + self.setup_ui() + + def setup_ui(self) -> None: + """设置界面与测试功能""" + button_1 = QtWidgets.QCheckBox("按钮1", self) + button_2 = QtWidgets.QCheckBox("按钮2", self) + button_3 = QtWidgets.QCheckBox("按钮3", self) + button_4 = QtWidgets.QCheckBox("按钮4", self) + button_1.move(380, 100) + button_2.move(380, 150) + button_3.move(380, 200) + button_4.move(380, 250) + + # QCheckBox默认不开启排他性,可以同时选中多个 + # 下面手动开启按钮1~3的排他性,这3个按钮中只能同时选中1个 + button_1.setAutoExclusive(True) + button_2.setAutoExclusive(True) + button_3.setAutoExclusive(True) + + print(button_4.autoExclusive()) + + +if __name__ == "__main__": + app = QtWidgets.QApplication(sys.argv) + window = MyWidget() + window.show() + sys.exit(app.exec())