QCheckBox是一个选项按钮,可以打开(选中)或关闭(未选中)。复选框通常用于在应用程序中表示某些功能的启用或者禁用。
QCheckBox使用
QCheckBox继承关系
QCheckBox是继承于QAbstractButton,所以QCheckBox的一些属性也继承了QAbstractButton。所以复选框在Pyside6中可以看作是特殊的按键。更多关于QCheckBox的使用可以参考下面两个文档。
https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractButton.html
https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QCheckBox.html
QCheckBox的函数和信号
QCheckBox的使用比较简单,通常我们只需要读取QCheckBox的状态,即QCheckBox是否被选中。QCheckBox和QPushButton一样,也是有自己的信号,不过通常我们只需要用到clicked这个信号。
信号 | 作用 |
---|---|
clicked | 按键松开时触发,如果按键按下后鼠标拖动到按键区域以外然后释放则不会触发 |
pressed | 按键按下时触发 |
released | 按键松开时触发,即使按键按下后鼠标拖动到按键区域以外也会触发 |
toggled | 需要设置 setCheckable(true) 后再单击按钮才会触发该信号 |
QCheckBox例程
在例程中,会设置3个QChcekBox,当QCheckBox选中或者取消时,打印出QCheckBox的状态。
界面设计
首先打开designer软件,创建一个Main Window,并在主界面中放置3个QCheckBox。
QCheckBox绑定信号
self.ui.checkBox.clicked.connect(self.checkoutbox_func1) # 复选框1
self.ui.checkBox_2.clicked.connect(self.checkoutbox_func2) # 复选框2
self.ui.checkBox_3.clicked.connect(self.checkoutbox_func3) # 复选框3
def checkoutbox_func1(self):
if self.ui.checkBox.isChecked() == True: # 判断复选框状态
print("用户选择了苹果")
else:
print("用户没有选择苹果")
def checkoutbox_func2(self):
if self.ui.checkBox_2.isChecked() == True: # 判断复选框状态
print("用户选择了香蕉")
else:
print("用户没有选择香蕉")
def checkoutbox_func3(self):
if self.ui.checkBox_3.isChecked() == True: # 判断复选框状态
print("用户选择了葡萄")
else:
print("用户没有选择葡萄")
完整程序
界面程序
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>282</width>
<height>217</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
</size>
</property>
<property name="sizeIncrement">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>请选择喜欢的水果</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>苹果</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_2">
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>香蕉</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>葡萄</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>282</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
主程序
# Import Qt libraries
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
# Import UI developed in Qt Creator
from checkbox_ui import Ui_MainWindow # 导入界面
# Import PseudoSensor
# Import system tools and datetime
import sys
import statistics
import time
from datetime import datetime
# Create and start the Qt application
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# 设置界面为用户设计的界面
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.checkBox.clicked.connect(self.checkoutbox_func1) # 复选框1
self.ui.checkBox_2.clicked.connect(self.checkoutbox_func2) # 复选框2
self.ui.checkBox_3.clicked.connect(self.checkoutbox_func3) # 复选框3
def checkoutbox_func1(self):
if self.ui.checkBox.isChecked() == True: # 判断复选框状态
print("用户选择了苹果")
else:
print("用户没有选择苹果")
def checkoutbox_func2(self):
if self.ui.checkBox_2.isChecked() == True: # 判断复选框状态
print("用户选择了香蕉")
else:
print("用户没有选择香蕉")
def checkoutbox_func3(self):
if self.ui.checkBox_3.isChecked() == True: # 判断复选框状态
print("用户选择了葡萄")
else:
print("用户没有选择葡萄")
def closeAndExit(self):
sys.exit()
if __name__ == "__main__":
app = QApplication(sys.argv) # 初始化QApplication
# 初始化界面并显示界面
window = MainWindow()
window.setFixedSize(window.width(), window.height())
window.show()
sys.exit(app.exec())