基础输入
CheckBox 复选框
选择一个或多个选项;半选状态表示只选中了一部分。
示例






用法
响应勾选
连接 toggled 或 stateChanged,在勾选或取消时作出响应。
#include <FluentQt/BasicInput.h>
using namespace fluent::basicinput;
auto* checkBox = new CheckBox("Accept terms", this);
auto* status = new Label("State: Unchecked", this);
connect(checkBox, &CheckBox::stateChanged, status, [status](int state) {
status->setText(state == Qt::Checked ? "State: Checked" : "State: Unchecked");
});
auto* disabledOff = new CheckBox("Disabled off", this);
disabledOff->setEnabled(false);
auto* disabledOn = new CheckBox("Disabled on", this);
disabledOn->setChecked(true);
disabledOn->setEnabled(false);import fluentqt
from PySide6.QtCore import Qt
check_box = fluentqt.CheckBox("Accept terms")
status = fluentqt.Label("State: Unchecked")
check_box.stateChanged.connect(
lambda state: status.setText(
"State: Checked"
if Qt.CheckState(state) == Qt.CheckState.Checked
else "State: Unchecked"
)
)
disabled_off = fluentqt.CheckBox("Disabled off")
disabled_off.setEnabled(False)
disabled_on = fluentqt.CheckBox("Disabled on")
disabled_on.setChecked(True)
disabled_on.setEnabled(False)半选状态
调用 setTristate(true) 后,可用 Qt::PartiallyChecked 表示只选中了一部分。
#include <FluentQt/BasicInput.h>
using namespace fluent::basicinput;
auto* checkBox = new CheckBox("Enable selected items", this);
checkBox->setTristate(true);
checkBox->setCheckState(Qt::PartiallyChecked);
auto* status = new Label("State: Mixed", this);
connect(checkBox, &CheckBox::stateChanged, status, [status](int state) {
status->setText(state == Qt::Checked ? "State: Checked"
: state == Qt::PartiallyChecked
? "State: Mixed"
: "State: Unchecked");
});import fluentqt
from PySide6.QtCore import Qt
check_box = fluentqt.CheckBox("Enable selected items")
check_box.setTristate(True)
check_box.setCheckState(Qt.CheckState.PartiallyChecked)
status = fluentqt.Label("State: Mixed")
check_box.stateChanged.connect(
lambda state: status.setText(
"State: Checked"
if Qt.CheckState(state) == Qt.CheckState.Checked
else "State: Mixed"
if Qt.CheckState(state) == Qt.CheckState.PartiallyChecked
else "State: Unchecked"
)
)全选
主复选框随子选项显示全选、未选或半选。
#include <FluentQt/BasicInput.h>
using namespace fluent::basicinput;
auto* selectAll = new CheckBox("Select all", this);
selectAll->setTristate(true);
auto* mail = new CheckBox("Mail", this);
auto* calendar = new CheckBox("Calendar", this);
auto* people = new CheckBox("People", this);
auto updateSelectAll = [=]() {
const int checked = int(mail->isChecked()) + int(calendar->isChecked()) + int(people->isChecked());
selectAll->setCheckState(checked == 0 ? Qt::Unchecked : checked == 3 ? Qt::Checked : Qt::PartiallyChecked);
};
connect(selectAll, &CheckBox::clicked, this, [=]() {
const bool checked = selectAll->checkState() == Qt::Checked;
mail->setChecked(checked);
calendar->setChecked(checked);
people->setChecked(checked);
});
connect(mail, &CheckBox::clicked, this, updateSelectAll);
connect(calendar, &CheckBox::clicked, this, updateSelectAll);
connect(people, &CheckBox::clicked, this, updateSelectAll);import fluentqt
from PySide6.QtCore import Qt
select_all = fluentqt.CheckBox("Select all")
select_all.setTristate(True)
mail = fluentqt.CheckBox("Mail")
calendar = fluentqt.CheckBox("Calendar")
people = fluentqt.CheckBox("People")
def update_select_all():
checked = sum(item.isChecked() for item in (mail, calendar, people))
state = (
Qt.CheckState.Unchecked if checked == 0
else Qt.CheckState.Checked if checked == 3
else Qt.CheckState.PartiallyChecked
)
select_all.setCheckState(state)
def apply_select_all():
checked = select_all.checkState() == Qt.CheckState.Checked
mail.setChecked(checked)
calendar.setChecked(checked)
people.setChecked(checked)
select_all.clicked.connect(apply_select_all)
mail.clicked.connect(update_select_all)
calendar.clicked.connect(update_select_all)
people.clicked.connect(update_select_all)API
查看声明没有匹配的 API 成员。
fluent::basicinput::CheckBox
继承自 QCheckBox · FluentElement · QMLPlus
带选中态动画的 Fluent 复选框。
构造函数
属性
qreal checkProgress归一化的选中态动画进度。
读取checkProgress()设置setCheckProgress()int boxSize复选框方框尺寸,单位为像素。
读取boxSize()设置setBoxSize()通知boxSizeChanged()int boxMargin复选框方框左侧前置间距。
读取boxMargin()设置setBoxMargin()通知boxMarginChanged()int textGap控件图形与文本内容之间的间距。
读取textGap()设置setTextGap()通知textGapChanged()bool hoverBackgroundEnabled悬停时是否绘制轻量背景。
读取hoverBackgroundEnabled()设置setHoverBackgroundEnabled()通知hoverBackgroundEnabledChanged()
方法
qreal checkProgress() const归一化的选中态动画进度。
void setCheckProgress(qreal progress)归一化的选中态动画进度。
int boxSize() const复选框方框尺寸,单位为像素。
void setBoxSize(int size)复选框方框尺寸,单位为像素。
int boxMargin() const复选框方框左侧前置间距。
void setBoxMargin(int margin)复选框方框左侧前置间距。
int textGap() const控件图形与文本内容之间的间距。
void setTextGap(int gap)控件图形与文本内容之间的间距。
bool hoverBackgroundEnabled() const悬停时是否绘制轻量背景。
void setHoverBackgroundEnabled(bool enabled)悬停时是否绘制轻量背景。
QSize sizeHint() const override提供给布局系统的尺寸提示。
信号
void boxSizeChanged()复选框方框尺寸,单位为像素。
void boxMarginChanged()复选框方框左侧前置间距。
void textGapChanged()控件图形与文本内容之间的间距。
void hoverBackgroundEnabledChanged()悬停时是否绘制轻量背景。
子类扩展接口
方法
继承自 fluent::FluentElement
方法
static void setTheme(Theme theme)更新全局主题,并通知所有活跃的 FluentElement 实例。
static void setThemeDeferred(Theme theme)以可见元素优先的批次调度主题刷新,避免阻塞当前交互。
static Theme currentTheme()返回当前生效的全局主题。
static bool themeUsesDarkAppearance(Theme theme)返回指定主题是否使用暗色底的 chrome 与材质 token。
static void refreshTheme()在设计 token 变化后重新广播当前主题以触发重绘。
static int themeGeneration()每次主题变化自增的单调计数器,用于过期判断。
Theme effectiveTheme() const解析此元素实际使用的主题,会优先读取 QWidget 父链上的局部主题覆盖。
bool effectiveThemeUsesDarkAppearance() const返回此元素的实际主题是否使用暗色底 chrome。
bool setThemeOverrides(const QJsonObject& overrides)供组件侧访问的设计 token 接口。
QJsonObject themeOverrides() const返回当前元素的覆盖配置。
void clearThemeOverrides()恢复全部 token 跟随全局主题。
const Colors& themeColorsRef() const对当前色板的零拷贝访问(用于绘制热路径)。
Material::AcrylicToken themeAcrylic() const材质、阴影和自适应布局 token。
QColor themeBackdrop(bool active) const给定激活状态下的窗口 chrome 背景色。
virtual void onThemeUpdated()全局主题变化后调用。
枚举
enum Theme解析设计 token 时使用的全局视觉主题。
Light = 0Dark = 1HighContrast = 2
类型与别名
struct Colors返回给组件使用的设计 token 快照。
继承自 fluent::QMLPlus
方法
AnchorLayout::Anchors* anchors()返回 QWidget 宿主的可变锚点配置。
void setState(const QString& name)应用具名 state;名称为空时恢复默认状态。
void addState(const QMLState& state)注册或替换一个具名 state 定义。
void evaluateStates()重新计算条件;按注册顺序选择首个成立的条件。
QMLPlusRuntime* runtime()返回按需创建、由宿主持有的状态观察对象。
void setAnchors(const AnchorLayout::Anchors& anchors)替换宿主的全部锚点,并使父布局失效。
void resetAnchors()清除宿主全部锚点,并使父布局失效。
void bind(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction dir = PropertyBinder::OneWay)将 source 属性绑定到 QWidget 宿主上的 target 属性。
BindingHandle bindManaged(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction direction = PropertyBinder::OneWay, BindingOptions options = {})在当前 QWidget 宿主上安装可管理的直接绑定。
BindingHandle bindComputed(const char* targetProp, const QVector<BindingDependency>& dependencies, std::function<QVariant()> evaluate, BindingOptions options = {})在当前 QWidget 宿主上安装使用显式依赖的 C++ 表达式绑定。
void setBehavior(const char* propertyName, const PropertyAnimation& animation)设置宿主受管理属性写入时使用的默认动画。
void clearBehavior(const char* propertyName)移除后续受管理写入使用的默认动画。
bool writeProperty(const char* propertyName, const QVariant& value)通过 Behavior 写入宿主属性,且不移除该属性的绑定。
Qt 基类完整成员:QCheckBox