Qt 控件 QSS 样式大全(控件特性篇)本篇聚焦各类 Qt 控件在 QSS 中的 特有样式入口、伪元素/子控件、常见视觉需求 和 实战代码片段,可与《通用属性篇》联动使用,快速搭建完整主题。
如何使用本文档- 定位控件:通过分类导览或搜索控件名称,找到对应章节。
- 阅读顺序:每节均按“结构剖析 → 关键属性 → 伪状态 → 实战示例 → 进阶技巧”展开,必要时提供对照表。
- 配套工具:结合 Qt Designer/Creator 或自制 Demo 工程,验证样式表影响;遇到子控件命名不清晰时,打开 Qt 文档或使用
setStyleSheet("* { border: 1px solid red; }") 方式定位。
分类导览| 模块 | 涵盖控件 | 核心样式入口 | 典型场景 |
|---|
| 1. 按钮与动作 | QAbstractButton、QPushButton、QToolButton、QCommandLinkButton | 主体、::menu-indicator、::indicator | 主按钮、图标按钮、菜单按钮、链接按钮 | | 2. 切换控件 | QCheckBox、QRadioButton、自定义开关 | ::indicator、:checked、:indeterminate | 复选框、单选、仿 iOS 开关 | | 3. 文本与表单输入 | QLineEdit、QTextEdit、QPlainTextEdit、QSpinBox 等 | 主体、::down-arrow、::up-button | 表单输入、搜索框、数值选择 | | 4. 下拉与选择器 | QComboBox、QFontComboBox、QDateEdit | ::drop-down、::down-arrow、QAbstractItemView | 下拉菜单、日期选择 | | 5. 滑动与滚动 | QSlider、QScrollBar、QDial | ::groove、::handle、::add-line | 滑块、滚动条、旋钮 | | 6. 进度与状态 | QProgressBar、QLCDNumber、QProgressDialog | ::chunk、::item | 进度条、步骤状态展示 | | 7. 视图与表头 | QListView、QTreeView、QTableView、QHeaderView | ::item、::section、:selected | 列表、树、表格主题化 | | 8. 容器与结构 | QGroupBox、QFrame、QTabWidget、QScrollArea | ::title、::pane、::tab | 面板、分页、滚动容器 | | 9. 菜单与工具栏 | QMenuBar、QMenu、QStatusBar、QToolBar | ::item、::separator | 顶部菜单、上下文菜单、状态栏 | | 10. 对话与提示 | QDialog、QMessageBox、QToolTip、QStatusTipEvent | 主体、::label、::button | 提示气泡、消息框、对话按钮 |
术语说明:本文使用 “子控件/伪元素” 指代 QSS 中带 :: 的选择器,如 QComboBox::drop-down;“伪状态” 指代 :hover、:checked 等状态选择器。
1. 按钮与动作控件1.1 QAbstractButton 通用特性结构剖析- 主控件:
QAbstractButton(基类,派生 QPushButton、QToolButton 等) - 子控件/伪元素:
::menu-indicator:带菜单按钮的下拉箭头区域。::indicator:适用于复选/单选等派生类。
- 常用伪状态:
:hover、:pressed、:checked、:disabled、:focus、:default、:flat。
关键属性与效果| 选择器/属性 | 功能说明 | 取值示例 | 使用提示 |
|---|
QAbstractButton | 控制按钮主体 | padding: 6px 14px; | 建议统一最小尺寸,提升触控体验 | QAbstractButton:hover | 悬停态反馈 | background: rgba(37, 99, 235, 0.08); | 与 :pressed 结合提供层级变化 | QAbstractButton:pressed | 按下态 | padding-top: 7px; | 通过内边距变化模拟下沉感 | QAbstractButton:default | 默认按钮样式 | border-color: #2563eb; | QPushButton::setDefault(true) 时触发 | QAbstractButton:disabled | 禁用态 | color: rgba(47, 53, 66, 0.45); | 降低对比度或透明度提醒不可用 | QAbstractButton::menu-indicator | 菜单箭头区域 | image: url(:/icons/chevron-down.svg); | 可设置 width/height/padding 调整对齐 | QAbstractButton::indicator | 带勾选功能的指示器 | width: 14px; height: 14px; | 适用于 QToolButton 的 checkable 模式 | QAbstractButton + icon-size | 控制图标尺度 | icon-size: 20px 20px; | 保持图标按钮视觉一致 | qproperty-icon | 设置按钮主图标 | qproperty-icon: url(:/icons/add.svg); | 适合纯图标按钮或工具栏 | QAbstractButton:flat | 扁平化状态 | background: transparent; | setFlat(true) 时触发,可重新定义悬停态 |
实战示例:主题化按钮基线
QAbstractButton {
min-height: 34px;
min-width: 88px;
padding: 6px 18px;
border-radius: 8px;
border: 1px solid transparent;
font: 500 14px "Microsoft YaHei";
}
QAbstractButton:default {
border-color: #2563eb;
background-color: rgba(37, 99, 235, 0.12);
}
QAbstractButton:pressed {
padding-top: 7px;
padding-bottom: 5px;
}
QAbstractButton::menu-indicator {
width: 12px;
image: url(:/icons/chevron-down.svg);
subcontrol-origin: padding;
subcontrol-position: right center;
margin-left: 6px;
}
进阶技巧- 使用
setProperty("tone", "primary") + QAbstractButton[tone="primary"] 实现多色主题。 - 对
::menu-indicator 使用 transform 可自定义旋转动画(Qt 6.5+)。 :checked 可制造“保持高亮”的 Toggle Button,配合 QButtonGroup 互斥模式。
1.2 QPushButton特有结构:default、:flat 状态常与原生风格绑定,需明确覆盖策略。::menu-indicator 默认尺寸较小,可通过 padding 和 subcontrol-origin 调整。- 支持
icon-size、qproperty-icon 快速替换图标。
常用伪状态扩展| 状态 | 触发条件 | 样式建议 |
|---|
:default | setDefault(true) | 通过边框或光晕强化主按钮 | :hover | 鼠标悬停 | 提升背景亮度或添加描边提醒可点击 | :checked | setCheckable(true) 被选中 | 用作 Toggle、Segmented 按钮 | :pressed | 鼠标按下 | 轻微下沉或阴影减弱,保持按钮反馈一致 | :focus | 键盘聚焦 | 结合 outline 或边框色提升键盘导航可见性 | :disabled | setEnabled(false) | 降低不透明度、禁用指针事件 | :menu-indicator:open | 菜单展开 | 改变箭头颜色或旋转指示当前打开状态 |
典型样式片段
QPushButton[tone="primary"] {
background-color: #2563eb;
border-color: #2563eb;
color: #ffffff;
}
QPushButton[tone="primary"]:hover {
background-color: #1d4ed8;
}
QPushButton[tone="primary"]:disabled {
background-color: rgba(37, 99, 235, 0.28);
border-color: transparent;
color: rgba(255, 255, 255, 0.65);
}
QPushButton[tone="ghost"] {
background: transparent;
border-color: rgba(37, 99, 235, 0.55);
color: #2563eb;
}
QPushButton[tone="ghost"]:hover {
background-color: rgba(37, 99, 235, 0.12);
}
常见问题- macOS 原生风格会覆盖部分圆角和阴影,可在
main.cpp 中指定 QApplication::setStyle("fusion") 保持一致。 - 若按钮位于
QToolBar,默认是 QToolButton 风格,需要单独匹配选择器。
1.3 QToolButton结构亮点- 既可表现为普通按钮,也可展示下拉菜单、工具栏按钮。
- 子控件:
::menu-button(类似拆分按钮)、::menu-indicator、::popup。 - 支持
ToolButtonTextUnderIcon、TextBesideIcon 等模式,可通过 toolButtonStyle 调整图文排布。
样式要点| 选择器 | 说明 | 示例 |
|---|
QToolButton | 主体 | padding: 6px; border-radius: 6px; | QToolButton:hover | 悬停高亮 | background-color: rgba(37, 99, 235, 0.12); | QToolButton:pressed | 按下压感 | background-color: rgba(37, 99, 235, 0.18); | QToolButton:checked | 切换按钮激活状态 | background-color: rgba(37, 99, 235, 0.2); | QToolButton:disabled | 禁用态 | color: rgba(51, 65, 85, 0.45); | QToolButton[autoRaise="true"] | 工具栏扁平风格 | background: transparent; border: none; | QToolButton[popupMode="MenuButtonPopup"] | 拆分按钮整体 | padding-right: 22px; | QToolButton[popupMode="MenuButtonPopup"]::menu-button | 拆分按钮右半部分 | width: 16px; border-left: 1px solid rgba(37, 99, 235, 0.2); | QToolButton::menu-button:pressed | 菜单按钮按下反馈 | background: rgba(37, 99, 235, 0.24); | QToolButton::menu-indicator | 菜单箭头 | image: url(:/icons/chevron-down.svg); | QToolButton::menu-indicator:open | 菜单展开态 | image: url(:/icons/chevron-up.svg); | QToolButton::popup | 弹出菜单面板容器 | padding: 4px; border-radius: 8px; |
QToolButton {
icon-size: 18px 18px;
padding: 6px;
border-radius: 6px;
background-color: transparent;
}
QToolButton:hover {
background-color: rgba(37, 99, 235, 0.12);
}
QToolButton:pressed {
background-color: rgba(37, 99, 235, 0.18);
}
QToolButton:checked {
background-color: rgba(37, 99, 235, 0.2);
border: 1px solid rgba(37, 99, 235, 0.25);
}
QToolButton:disabled {
background-color: transparent;
color: rgba(51, 65, 85, 0.45);
}
QToolButton::menu-button {
border-left: 1px solid rgba(37, 99, 235, 0.2);
width: 16px;
background: transparent;
}
QToolButton::menu-button:pressed {
background: rgba(37, 99, 235, 0.24);
}
QToolButton::menu-indicator {
image: url(:/icons/chevron-down.svg);
}
QToolButton::menu-indicator:open {
image: url(:/icons/chevron-up.svg);
}
1.4 QCommandLinkButton- 内置标题与描述两行文本,对应子控件
::description。 - 默认带箭头图标,可通过
icon 自定义或隐藏。 - 支持
:hover、:pressed、:default、:disabled 状态。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QCommandLinkButton | 主按钮外观 | padding: 12px 18px; border-radius: 10px; | QCommandLinkButton:hover | 悬停态 | background-color: rgba(37,99,235,0.08); | QCommandLinkButton:pressed | 按压态 | background-color: rgba(37,99,235,0.14); | QCommandLinkButton:default | 默认按钮 | border-color: #2563eb; | QCommandLinkButton:disabled | 禁用态 | color: rgba(71,85,105,0.45); | QCommandLinkButton::description | 副标题文本 | font-size: 12px; color: rgba(...); | QCommandLinkButton::icon | 左侧图标 | image: url(:/icons/arrow-right.svg); | qproperty-description | 代码注入描述文本 | button->setProperty("description", ...) | qproperty-iconSize | 描述图标尺寸(Qt 6) | qproperty-iconSize: QSize(32,32); |
QCommandLinkButton {
padding: 12px 18px;
border-radius: 10px;
border: 1px solid rgba(148, 163, 184, 0.35);
color: #0f172a;
font-weight: 500;
}
QCommandLinkButton::description {
color: rgba(71, 85, 105, 0.85);
font-size: 12px;
margin-top: 4px;
}
QCommandLinkButton:hover {
background-color: rgba(37, 99, 235, 0.08);
}
QCommandLinkButton:pressed {
background-color: rgba(37, 99, 235, 0.14);
}
QCommandLinkButton:disabled {
color: rgba(71, 85, 105, 0.45);
}
2. 切换控件(Checkable Controls)2.1 QCheckBox结构剖析- 主体:
QCheckBox - 子控件:
::indicator(复选框图标)、::indicator:unchecked、::indicator:checked、::indicator:indeterminate - 常见伪状态:
:hover、:pressed、:disabled、:focus、:indicator:hover
关键属性| 选择器 | 功能 | 取值示例 | 提示 |
|---|
QCheckBox | 控制文字区域 | spacing: 8px; padding: 4px; | spacing 控制文本与指示器间距 | QCheckBox:focus | 键盘聚焦 | outline: none; + border | 可结合 :focus 自定义外框 | QCheckBox:disabled | 禁用态 | color: rgba(71,85,105,0.45); | 同步调整指示器禁用配色 | QCheckBox::indicator | 调整指示器尺寸 | width: 16px; height: 16px; | 需设定背景、边框或图片 | QCheckBox::indicator:unchecked | 未勾选 | background: #fff; | 可定制为空心方框或浅灰底 | QCheckBox::indicator:hover | 悬停态 | border-color: #2563eb; | 强调可勾选区域 | QCheckBox::indicator:pressed | 按下态 | background-color: rgba(37,99,235,0.18); | 提供点击反馈 | QCheckBox::indicator:checked | 渲染勾选状态 | image: url(:/icons/check.svg); | 图片需自带透明背景 | QCheckBox::indicator:checked:disabled | 禁用勾选 | background-color: rgba(148,163,184,0.35); | 保持状态但弱化视觉权重 | QCheckBox::indicator:indeterminate | 三态 | background-color: #2563eb; | 与 setTristate(true) 组合 |
实战:方形与圆形主题
QCheckBox {
spacing: 8px;
font-size: 14px;
}
QCheckBox::indicator {
width: 18px;
height: 18px;
border-radius: 4px;
border: 1px solid rgba(71, 85, 105, 0.45);
background-color: #fff;
}
QCheckBox::indicator:hover {
border-color: #2563eb;
}
QCheckBox::indicator:checked {
border-color: #2563eb;
background-color: #2563eb;
image: url(:/icons/check-small.svg);
}
QCheckBox::indicator:indeterminate {
border-color: #f59e0b;
background-color: #f59e0b;
image: url(:/icons/minus.svg);
}
进阶:自定义开关(Switch)- 利用
QCheckBox + 动态属性 appearance="switch"。 - 使用
::indicator 充当轨道,::indicator::checked 伪状态中通过 QBoxShadow 或背景渐变表达激活。 - 通过
subcontrol-position 或伪元素自绘滑块。
QCheckBox[appearance="switch"] {
padding: 4px 0;
}
QCheckBox[appearance="switch"]::indicator {
width: 40px;
height: 20px;
border-radius: 10px;
border: 1px solid rgba(148, 163, 184, 0.45);
background-color: rgba(226, 232, 240, 0.9);
image: url(:/switch/knob-left.png);
}
QCheckBox[appearance="switch"]::indicator:hover {
border-color: rgba(37, 99, 235, 0.65);
}
QCheckBox[appearance="switch"]::indicator:checked {
border-color: #22c55e;
background-color: rgba(34, 197, 94, 0.35);
image: url(:/switch/knob-right.png);
}
QCheckBox[appearance="switch"]::indicator:disabled {
border-color: rgba(148, 163, 184, 0.25);
background-color: rgba(148, 163, 184, 0.15);
image: url(:/switch/knob-disabled.png);
}
若需无资源依赖的圆点动画,可在代码中组合 QStateMachine 或自绘;单纯使用 QSS 时,可利用两套 PNG/SVG 表现滑块位置,或改用自定义 QWidget。
2.2 QRadioButton- 与
QCheckBox 相似,但 ::indicator 通常为圆形。 - 可通过
QButtonGroup 设置互斥。
QRadioButton::indicator {
width: 16px;
height: 16px;
border-radius: 8px;
border: 1px solid rgba(71, 85, 105, 0.45);
background-color: #fff;
}
QRadioButton::indicator:checked {
border-color: #2563eb;
background-color: #2563eb;
image: url(:/icons/radio-dot.svg);
}
3. 文本与表单输入控件3.1 QLineEdit结构剖析- 主体:
QLineEdit - 子控件:
::placeholder(Qt 5.12+),::clear-button(Qt 5.2+,启用 setClearButtonEnabled(true)) - 伪状态:
:focus、:read-only、:disabled、:hover
关键属性| 选择器 | 功能 | 示例 | 小贴士 | |
|---|
QLineEdit | 背景、边框、内边距 | padding: 6px 10px; | 保留足够空间容纳清除按钮 | | QLineEdit:hover | 悬停态 | border-color: rgba(37,99,235,0.45); | 用浅色边框暗示可编辑 | | QLineEdit:focus | 高亮状态 | border-color: #2563eb; | 可叠加 outline 提示键盘焦点 | | QLineEdit:read-only | 只读样式 | background-color: #f8fafc; | 通过颜色差异提示不可编辑 | | QLineEdit:disabled | 禁用样式 | color: rgba(71,85,105,0.45); | 同时调整边框与背景强调不可用 | | QLineEdit[echoMode="Password"] | 密码模式 | lineedit[echoMode="Password"] { letter-spacing: 2px; } | 通过字符间距增强可读性 | | QLineEdit[frame="false"] | 去除边框 | border: none; | 常与嵌套容器搭配自定义边框 | | QLineEdit::placeholder | 占位文本 | color: rgba(71,85,105,0.65); | Qt 5.12 前需改用 setPlaceholderText + 样式表 trick | | QLineEdit::selection | 选中文本 | background-color: rgba(37,99,235,0.2); | 可与 selection-color 搭配 | | QLineEdit::clear-button | 清除按钮 | image: url(:/icons/close-circle.svg); | 设置 subcontrol-position: right center; 调整位置 | | qproperty-alignment | 文本对齐 | `qproperty-alignment: AlignRight | AlignVCenter;` | 做金额输入或右对齐时使用 | qproperty-completer | 自动补全 | qproperty-completer: completerInstance; | 需在代码中设置对象名并注入 | |
QLineEdit {
padding: 6px 12px;
border: 1px solid rgba(148, 163, 184, 0.65);
border-radius: 6px;
background-color: #ffffff;
selection-background-color: rgba(37, 99, 235, 0.2);
}
QLineEdit:focus {
border-color: #2563eb;
background-color: #f1f5ff;
}
QLineEdit::placeholder {
color: rgba(100, 116, 139, 0.7);
}
QLineEdit::clear-button {
image: url(:/icons/close-circle.svg);
width: 16px;
height: 16px;
}
3.2 QTextEdit / QPlainTextEdit- 支持滚动条子控件
QScrollBar。需要单独样式。 QTextEdit 支持富文本,QPlainTextEdit 用于纯文本,QSS 入口基本一致。- 可使用
line-height(Qt 6.2+)调整行距。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QTextEdit / QPlainTextEdit | 主体外观、背景 | padding: 10px 12px; border-radius: 8px; | QTextEdit:hover | 悬停提示 | border-color: rgba(37,99,235,0.45); | QTextEdit:focus | 焦点状态 | border-color: #2563eb; | QTextEdit:read-only | 只读样式 | background-color: #f8fafc; | QTextEdit:disabled | 禁用态 | color: rgba(71,85,105,0.45); | QTextEdit::placeholder(Qt 6.2+) | 占位文本 | color: rgba(100,116,139,0.6); | QTextEdit::corner | 右下角占位部件 | background: transparent; | QTextEdit QScrollBar:vertical | 垂直滚动条轨道 | width: 8px; margin: 4px; | QTextEdit QScrollBar::handle:vertical | 垂直滚动条滑块 | background: rgba(148,163,184,0.65); | QTextEdit QScrollBar:horizontal | 水平滚动条 | height: 8px; | QPlainTextEdit[tabChangesFocus="true"] | Tab 转换焦点的文本编辑器 | background-color: #ffffff; | qproperty-lineWrapMode | 控制换行模式(代码设置) | editor->setLineWrapMode(QTextEdit::NoWrap); | qproperty-overwriteMode | 置换模式指示 | editor->setOverwriteMode(true); |
QTextEdit {
border: 1px solid rgba(148, 163, 184, 0.65);
border-radius: 8px;
padding: 10px 12px;
background-color: #ffffff;
selection-background-color: rgba(37, 99, 235, 0.18);
}
QTextEdit:focus {
border-color: #2563eb;
background-color: #f8fbff;
}
QTextEdit:read-only {
background-color: rgba(241, 245, 249, 0.85);
}
QTextEdit QScrollBar:vertical {
width: 8px;
margin: 4px;
background: transparent;
}
QTextEdit QScrollBar::handle:vertical {
min-height: 32px;
border-radius: 4px;
background-color: rgba(148, 163, 184, 0.65);
}
QTextEdit QScrollBar:horizontal {
height: 8px;
margin: 0 4px;
background: transparent;
}
QTextEdit QScrollBar::handle:horizontal {
min-width: 32px;
border-radius: 4px;
background-color: rgba(148, 163, 184, 0.65);
}
3.3 QSpinBox / QDoubleSpinBox- 子控件:
::up-button、::down-button、::up-arrow、::down-arrow。 - 可通过
padding-right 留出按钮空间。 - 支持
:up-button:pressed、:down-button:pressed、:disabled 等状态。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QSpinBox / QDoubleSpinBox | 主体外观 | padding: 4px 24px 4px 10px; | QSpinBox:hover | 悬停状态 | border-color: rgba(37,99,235,0.45); | QSpinBox:focus | 聚焦状态 | border-color: #2563eb; | QSpinBox:disabled | 禁用态 | color: rgba(71,85,105,0.45); | QSpinBox::up-button | 增量按钮容器 | width: 20px; subcontrol-position: top right; | QSpinBox::up-button:pressed | 增量按钮按下 | background: rgba(37,99,235,0.18); | QSpinBox::down-button | 减量按钮容器 | width: 20px; subcontrol-position: bottom right; | QSpinBox::down-button:pressed | 减量按钮按下 | background: rgba(37,99,235,0.18); | QSpinBox::up-arrow / ::down-arrow | 箭头图标 | image: url(:/icons/chevron-up.svg); | QSpinBox[buttonSymbols="NoButtons"] | 隐藏按钮(仅数值输入) | border: 1px solid ...; padding-right: 10px; | QSpinBox::up-button:disabled | 禁用按钮 | background: transparent; image: none; | qproperty-prefix / qproperty-suffix | 前缀/后缀文本 | spin->setPrefix("¥"); |
QSpinBox {
padding: 4px 24px 4px 10px;
border: 1px solid rgba(148, 163, 184, 0.65);
border-radius: 6px;
background-color: #ffffff;
}
QSpinBox:focus {
border-color: #2563eb;
}
QSpinBox::up-button {
subcontrol-origin: border;
subcontrol-position: top right;
width: 20px;
border-left: 1px solid rgba(148, 163, 184, 0.35);
background-color: rgba(148, 163, 184, 0.08);
}
QSpinBox::up-arrow {
image: url(:/icons/chevron-up.svg);
width: 10px;
height: 10px;
}
QSpinBox::down-arrow {
image: url(:/icons/chevron-down.svg);
width: 10px;
height: 10px;
}
QSpinBox::down-button {
subcontrol-origin: border;
subcontrol-position: bottom right;
width: 20px;
border-left: 1px solid rgba(148, 163, 184, 0.35);
background-color: rgba(148, 163, 184, 0.08);
}
QSpinBox::up-button:pressed,
QSpinBox::down-button:pressed {
background-color: rgba(37, 99, 235, 0.18);
}
QSpinBox::up-arrow:disabled,
QSpinBox::down-arrow:disabled {
image: none;
}
3.4 QDateEdit / QTimeEdit / QDateTimeEdit- 派生自
QAbstractSpinBox,包含下拉日历。 - 子控件
::drop-down 控制下拉按钮,::down-arrow 控制箭头。 - 下拉日历是
QCalendarWidget,需分别设置其子控件。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QDateEdit / QTimeEdit | 主体外观 | padding: 4px 28px 4px 10px; border-radius: 6px; | QDateEdit:focus | 聚焦态 | border-color: #2563eb; | QDateEdit:disabled | 禁用态 | color: rgba(71,85,105,0.45); | QDateEdit::drop-down | 下拉按钮区域 | width: 24px; border-left: 1px solid rgba(...); | QDateEdit::drop-down:pressed | 下拉按钮按压 | background: rgba(37,99,235,0.18); | QDateEdit::down-arrow | 箭头图标 | image: url(:/icons/calendar.svg); | QDateEdit[calendarPopup="true"] | 启用日历弹窗 | background-color: #ffffff; | QDateEdit::up-button / ::down-button | 时间编辑器增减按钮 | width: 18px; subcontrol-position: top right; | QCalendarWidget | 日历整体容器 | border-radius: 8px; padding: 12px; | QCalendarWidget QToolButton | 年/月切换按钮 | icon-size: 16px; border-radius: 6px; | QCalendarWidget QToolButton:pressed | 按压态 | background: rgba(37,99,235,0.18); | QCalendarWidget QAbstractItemView::item | 日期单元格 | min-height: 28px; | QCalendarWidget QAbstractItemView::item:hover | 悬停单元 | background: rgba(37,99,235,0.1); | QCalendarWidget QWidget#qt_calendar_navigationbar | 顶部导航栏 | background: rgba(148,163,184,0.12); | qproperty-displayFormat | 日期时间显示格式(代码) | edit->setDisplayFormat("yyyy-MM-dd HH:mm"); |
QDateEdit {
padding: 4px 28px 4px 10px;
border: 1px solid rgba(148, 163, 184, 0.65);
border-radius: 6px;
background-color: #ffffff;
}
QDateEdit:focus {
border-color: #2563eb;
}
QDateEdit::drop-down {
subcontrol-origin: padding;
subcontrol-position: right center;
width: 24px;
border-left: 1px solid rgba(148, 163, 184, 0.35);
background-color: rgba(148, 163, 184, 0.12);
}
QDateEdit::drop-down:pressed {
background-color: rgba(37, 99, 235, 0.18);
}
QDateEdit::down-arrow {
image: url(:/icons/calendar.svg);
width: 12px;
height: 12px;
}
QCalendarWidget {
background-color: #ffffff;
border: 1px solid rgba(148, 163, 184, 0.45);
border-radius: 8px;
padding: 12px;
}
QCalendarWidget QWidget#qt_calendar_navigationbar {
background-color: rgba(148, 163, 184, 0.12);
border-radius: 6px;
}
QCalendarWidget QToolButton {
icon-size: 16px;
border: none;
padding: 4px;
border-radius: 6px;
}
QCalendarWidget QToolButton:hover {
background-color: rgba(37, 99, 235, 0.12);
}
QCalendarWidget QAbstractItemView:enabled {
selection-background-color: rgba(37, 99, 235, 0.2);
selection-color: #1d4ed8;
outline: none;
}
QCalendarWidget QAbstractItemView::item {
min-height: 28px;
margin: 2px 4px;
}
QCalendarWidget QAbstractItemView::item:hover {
background-color: rgba(37, 99, 235, 0.1);
}
QCalendarWidget QTableView::item:selected:!active {
background-color: rgba(37, 99, 235, 0.18);
}
4. 下拉与选择器4.1 QComboBox结构剖析- 主体:
QComboBox - 子控件:
::drop-down(下拉按钮区域)、::down-arrow(箭头)、::indicator(可编辑模式下的下拉标记)、::item(Qt
6 支持) - 弹出视图:
QComboBox QAbstractItemView,通常为 QListView
属性对照| 选择器 | 功能 | 示例 | 说明 |
|---|
QComboBox | 控制外观 | padding: 4px 28px 4px 10px; | 需留出下拉按钮宽度 | QComboBox:hover | 悬停态 | border-color: rgba(37,99,235,0.45); | 鼠标悬停时浅色描边 | QComboBox:focus | 聚焦态 | border-color: #2563eb; | 支持键盘导航高亮 | QComboBox:editable | 可编辑模式 | background: #ffffff; | 内含 QLineEdit,需同步样式 | QComboBox::drop-down | 定义按钮区域 | width: 24px; border-left: 1px solid ... | 可设置背景、圆角 | QComboBox::drop-down:pressed | 按下态 | background: rgba(37,99,235,0.18); | 显示交互反馈 | QComboBox::down-arrow | 箭头图标 | image: url(:/icons/chevron-down.svg); | 可替换为自定义 SVG | QComboBox::indicator | 可编辑下拉指示 | image: url(:/icons/menu.svg); | 仅在 editable 模式可见 | QComboBox:on | 下拉展开状态 | border-color: #2563eb; | 用于高亮展开中的控件 | QComboBox::item(Qt 6) | 主题化弹出项 | padding: 6px 14px; | 直接在 QSS 控制列表项外观 | QComboBox QListView::item | 列表项 | min-height: 28px; | Qt 5 可通过视图类匹配 | QComboBox QListView::item:selected | 选中项 | background: rgba(37,99,235,0.18); | 强调当前选项 | QComboBox QAbstractItemView | 弹出列表 | border-radius: 8px; | 可统一滚动条、选中样式 | QComboBox::separator | 分隔线 | height: 1px; background: rgba(148,163,184,0.35); | 需启用插入分隔符的模型 |
QComboBox {
padding: 4px 28px 4px 10px;
border: 1px solid rgba(148, 163, 184, 0.65);
border-radius: 6px;
}
QComboBox::drop-down {
subcontrol-origin: padding;
subcontrol-position: right center;
width: 26px;
border-left: 1px solid rgba(148, 163, 184, 0.35);
background-color: rgba(148, 163, 184, 0.12);
}
QComboBox::down-arrow {
image: url(:/icons/chevron-down.svg);
width: 12px;
height: 12px;
}
QComboBox QAbstractItemView {
padding: 4px 0;
border: 1px solid rgba(148, 163, 184, 0.45);
border-radius: 8px;
background-color: #ffffff;
selection-background-color: rgba(37, 99, 235, 0.12);
selection-color: #1d4ed8;
}
4.2 QFontComboBox| 选择器/属性 | 功能说明 | 示例 |
|---|
QFontComboBox | 主体外观 | min-width: 200px; | QFontComboBox::drop-down | 下拉按钮 | 与 4.1 节保持一致 | QFontComboBox QListView::item | 字体项 | min-height: 28px; font-size: 14px; | QFontComboBox QListView::item:hover | 悬停项 | background-color: rgba(37,99,235,0.12); | QFontComboBox QListView::item:selected | 选中项 | background-color: rgba(37,99,235,0.18); | QFontComboBox[fontFilters~="MonospacedFonts"] | 过滤字体 | fontCombo->setFontFilters(QFontComboBox::MonospacedFonts); | qproperty-currentFont | 当前字体(代码设置) | fontCombo->setCurrentFont(QFont("Fira Code")); |
4.3 可编辑组合框QComboBox 设置 setEditable(true) 后内部包含 QLineEdit,需同步设置。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QComboBox:editable | 可编辑模式主体 | background: #ffffff; | QComboBox:editable:on | 展开时 | border-color: #2563eb; | QComboBox::drop-down:editable | 下拉按钮 | image: none; | QComboBox QLineEdit | 内部行编辑 | border: none; padding: 4px 6px; | QComboBox QLineEdit:focus | 内部聚焦 | border: none; background: transparent; | QComboBox::indicator | 指示图标(可选) | image: url(:/icons/chevron-down.svg); |
QComboBox:editable {
background: #ffffff;
}
QComboBox::drop-down:editable {
image: none;
}
QComboBox QLineEdit {
border: none;
padding: 4px 6px;
}
5. 滑动与滚动控件5.1 QSlider- 子控件:
::groove(轨道)、::handle(滑块)、::add-page、::sub-page(水平/垂直)、::tick-position(刻度) - 伪状态:
:hover、:pressed、:disabled
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QSlider::groove:horizontal | 水平轨道 | height: 4px; border-radius: 2px; | QSlider::groove:vertical | 垂直轨道 | width: 4px; border-radius: 2px; | QSlider::sub-page | 已滑过区段 | background-color: #2563eb; | QSlider::add-page | 未滑过区段 | background-color: rgba(148,163,184,0.35); | QSlider::handle | 滑块 | width: 16px; border: 2px solid #2563eb; | QSlider::handle:hover | 滑块悬停 | border-color: #1d4ed8; | QSlider::handle:pressed | 滑块按压 | background-color: #2563eb; color: #fff; | QSlider:disabled | 禁用态 | opacity: 0.4;(Qt 6.6+) | QSlider::tick-position | 刻度条位置 | margin-top: 8px; | QSlider::handle:vertical | 垂直滑块尺寸 | width: 16px; height: 16px; margin: 0 -6px; | QSlider[orientation="Vertical"] | 纵向整体匹配 | min-height: 120px; | qproperty-pageStep / qproperty-singleStep | 步长(代码配置) | slider->setPageStep(10); |
QSlider::groove:horizontal {
height: 4px;
border-radius: 2px;
background-color: rgba(148, 163, 184, 0.35);
}
QSlider::sub-page:horizontal {
background-color: #2563eb;
border-radius: 2px;
}
QSlider::handle:horizontal {
width: 16px;
height: 16px;
margin: -6px 0;
border-radius: 8px;
background-color: #ffffff;
border: 2px solid #2563eb;
}
QSlider::handle:horizontal:hover {
border-color: #1d4ed8;
}
QSlider::handle:horizontal:pressed {
background-color: #2563eb;
color: #ffffff;
}
QSlider::groove:vertical {
width: 4px;
border-radius: 2px;
background-color: rgba(148, 163, 184, 0.35);
}
QSlider::sub-page:vertical {
background-color: #2563eb;
border-radius: 2px;
}
QSlider::handle:vertical {
width: 16px;
height: 16px;
margin: 0 -6px;
border-radius: 8px;
background-color: #ffffff;
border: 2px solid #2563eb;
}
5.2 QScrollBar- 子控件:
::handle、::add-line、::sub-line、::add-page、::sub-page、::corner - 可通过
qproperty-pageStep 控制滚动幅度(代码层)。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QScrollBar:vertical | 垂直轨道 | width: 8px; margin: 4px; | QScrollBar:horizontal | 水平轨道 | height: 8px; margin: 4px; | QScrollBar::handle | 滑块 | min-height: 32px; border-radius: 4px; | QScrollBar::handle:hover | 悬停 | background-color: rgba(37,99,235,0.65); | QScrollBar::handle:pressed | 按压 | background-color: rgba(37,99,235,0.85); | QScrollBar::add-line / ::sub-line | 顶/底按钮(分页按钮) | height: 0; 或设置箭头图标 | QScrollBar::add-page / ::sub-page | 未占用区域 | background: transparent; | QScrollBar::up-arrow / ::down-arrow | 箭头图标 | image: url(:/icons/chevron-up.svg); | QScrollBar::corner | 滚动条与滚动条交叉区域 | background: rgba(148,163,184,0.12); | QScrollBar:disabled | 禁用态 | background: transparent; | QScrollArea QWidget::viewport | 配合滚动区域背景 | background-color: #ffffff; | qproperty-singleStep | 单次滚动步长(代码) | scrollBar->setSingleStep(24); |
QScrollBar:vertical {
width: 8px;
background: transparent;
margin: 4px;
}
QScrollBar::handle:vertical {
min-height: 32px;
border-radius: 4px;
background-color: rgba(148, 163, 184, 0.65);
}
QScrollBar::handle:vertical:hover {
background-color: rgba(37, 99, 235, 0.65);
}
QScrollBar::add-line:vertical,
QScrollBar::sub-line:vertical {
height: 0px;
}
QScrollBar::add-page:vertical,
QScrollBar::sub-page:vertical {
background: transparent;
}
QScrollBar:horizontal {
height: 8px;
background: transparent;
margin: 0 4px;
}
QScrollBar::handle:horizontal {
min-width: 32px;
border-radius: 4px;
background-color: rgba(148, 163, 184, 0.65);
}
QScrollBar::handle:horizontal:hover {
background-color: rgba(37, 99, 235, 0.65);
}
QScrollBar::corner {
background: rgba(148, 163, 184, 0.12);
}
5.3 QDial- 支持
background、foreground、notch 颜色,QSS 能力有限。 - 高级样式建议使用
QProxyStyle 或自绘。
可用属性速查| 选择器/属性 | 功能说明 | 示例 |
|---|
QDial | 背景与外框 | background: #ffffff; border-radius: 50%; | QDial::handle | 指针/旋钮(Qt 6+) | image: url(:/dial/handle.svg); | QDial::notch | 刻度线颜色 | color: rgba(148,163,184,0.75); | QDial:focus | 聚焦态 | border: 2px solid #2563eb; | QDial:disabled | 禁用态透明度 | opacity: 0.4;(Qt 6.6+) | qproperty-notchTarget | 刻度数量(代码设置) | dial->setNotchTarget(36); | qproperty-wrapping | 是否允许循环(代码) | dial->setWrapping(true); |
6. 进度与状态反馈6.1 QProgressBar- 子控件:
::chunk 表示完成部分。 - 支持水平、垂直、文本位置 (
text-visible 属性)。
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QProgressBar | 主体容器 | min-height: 12px; text-align: center; | QProgressBar::chunk | 完成区块 | background: qlineargradient(...); | QProgressBar::chunk:indeterminate | 不确定状态动画(手动模拟) | background: url(:/patterns/stripe.png) repeat; | QProgressBar[textVisible="false"] | 隐藏文本 | padding: 0; | QProgressBar[orientation="Vertical"] | 垂直进度条 | min-width: 12px; | QProgressBar::chunk:disabled | 禁用态区块 | background-color: rgba(148,163,184,0.35); | QProgressBar[format="%p%"] | 百分比格式(代码配置) | progress->setFormat("%p% 已完成"); | QProgressBar::chunk:vertical | 垂直区块 | width: 100%; margin: 0; | QProgressBar::chunk:horizontal | 水平区块 | height: 100%; margin: 0; | QProgressBar[textDirection="BottomToTop"] | 文本方向(代码) | progress->setTextDirection(QProgressBar::BottomToTop); |
QProgressBar {
min-height: 12px;
border-radius: 6px;
background-color: rgba(148, 163, 184, 0.25);
text-align: center;
color: #1f2937;
font: 500 12px "Microsoft YaHei";
}
QProgressBar::chunk {
border-radius: 6px;
background: qlineargradient(
x1: 0,
y1: 0,
x2: 1,
y2: 0,
stop: 0 #2563eb,
stop: 1 #60a5fa
);
}
QProgressBar[chunkShape="striped"]::chunk {
background: url(:/patterns/stripe.png) repeat;
}
QProgressBar[orientation="Vertical"] {
min-width: 12px;
min-height: 120px;
}
QProgressBar[orientation="Vertical"]::chunk {
width: 100%;
}
6.2 QProgressDialog- 聚合进度条与文本,主体为
QDialog。 - 可针对
QProgressDialog QLabel、QProgressDialog QPushButton、内部 QProgressBar 分别设置。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QProgressDialog | 对话框外框 | border-radius: 12px; background: #ffffff; | QProgressDialog > QLabel | 文本区域 | font-size: 14px; color: #0f172a; | QProgressDialog > QPushButton | 取消按钮 | min-width: 88px; | QProgressDialog QProgressBar | 嵌入进度条 | 与 6.1 节保持一致 | QProgressDialog[minimumDuration=0] | 秒显模式(代码) | dialog->setMinimumDuration(0); | QProgressDialog[autoClose="false"] | 禁止自动关闭(代码) | dialog->setAutoClose(false); |
6.3 状态指示类控件QLCDNumber:可通过 color、background-color、segment-style 控制外观。QStatusBar 消息提示参见第 9 节。
7. 视图、表格与表头7.1 QListView / QTreeView- 子控件:
::item、::branch(QTreeView) - 支持
alternate-background-color、:selected、:hover、:disabled
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QListView / QTreeView | 主体容器 | border-radius: 8px; padding: 4px 0; | QListView::item | 列表项基础样式 | min-height: 32px; padding: 0 14px; | QListView::item:hover | 悬停态 | background-color: rgba(37,99,235,0.1); | QListView::item:selected | 选中态 | background-color: rgba(37,99,235,0.22); | QListView::item:selected:!active | 非活跃窗口选中 | background-color: rgba(37,99,235,0.12); | QTreeView::branch | 树枝节点 | image: url(:/icons/tree-closed.svg); | QTreeView::branch:open | 展开状态图标 | image: url(:/icons/tree-open.svg); | QTreeView::branch:has-siblings:adjoins-item | 多子节点缩进 | border-image: none; | QTreeView::indicator(可选) | 勾选框(若可选) | width: 14px; height: 14px; | QTreeView::item:selected:focus | 焦点与选中 | outline: none; | QListView::indicator(带复选模型时) | 自定义复选框 | image: url(:/icons/check.svg); | qproperty-uniformRowHeights | 优化树视图性能(代码) | tree->setUniformRowHeights(true); |
QListView {
border: 1px solid rgba(148, 163, 184, 0.45);
border-radius: 8px;
background-color: #ffffff;
padding: 4px 0;
}
QListView::item {
min-height: 32px;
padding: 0 14px;
}
QListView::item:hover {
background-color: rgba(37, 99, 235, 0.1);
}
QListView::item:selected {
background-color: rgba(37, 99, 235, 0.22);
color: #1d4ed8;
}
QListView::item:selected:!active {
background-color: rgba(37, 99, 235, 0.12);
color: #334155;
}
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings {
image: url(:/icons/tree-closed.svg);
}
QTreeView::branch:open:has-children {
image: url(:/icons/tree-open.svg);
}
QTreeView::item:selected {
background-color: rgba(37, 99, 235, 0.22);
color: #1d4ed8;
}
7.2 QTableView / QTableWidget- 子控件:水平/垂直表头
QHeaderView::section,交替行 alternate-background-color - 单元格使用
::item,支持 :selected、:focus、:hover
关键属性| 选择器/属性 | 功能说明 | 示例 |
|---|
QTableView | 表格主体 | gridline-color: rgba(148,163,184,0.35); | QTableView::item | 单元格样式 | padding: 6px 12px; | QTableView::item:selected | 选中单元 | background-color: rgba(37,99,235,0.18); | QTableView::item:focus | 焦点单元 | outline: 1px solid #2563eb; | QTableView::item:selected:!active | 非激活窗口选中 | background-color: rgba(37,99,235,0.12); | QHeaderView | 表头容器 | background-color: #f1f5f9; | QHeaderView::section | 表头单元 | padding: 8px 12px; border-right: 1px solid rgba(...); | QHeaderView::section:hover | 悬停表头 | background-color: rgba(148,163,184,0.18); | QHeaderView::section:checked | 勾选表头(列选择) | background-color: rgba(37,99,235,0.18); | QHeaderView::up-arrow / ::down-arrow | 排序箭头 | image: url(:/icons/arrow-up.svg); | QTableCornerButton::section | 左上角按钮 | background: #f1f5f9; border: none; | QTableView QTableCornerButton::section:pressed | 按下角按钮 | background-color: rgba(37,99,235,0.18); | qproperty-wordWrap | 单元格换行(代码配置) | table->setWordWrap(false); |
QTableView {
gridline-color: rgba(148, 163, 184, 0.35);
border: 1px solid rgba(148, 163, 184, 0.45);
selection-background-color: rgba(37, 99, 235, 0.18);
selection-color: #1d4ed8;
alternate-background-color: #f8fafc;
}
QTableView::item:selected:!active {
background-color: rgba(37, 99, 235, 0.12);
}
QHeaderView {
background-color: #f1f5f9;
}
QHeaderView::section {
padding: 8px 12px;
border: none;
border-right: 1px solid rgba(148, 163, 184, 0.45);
font-weight: 600;
}
QHeaderView::section:hover {
background-color: rgba(148, 163, 184, 0.18);
}
QHeaderView::section:checked {
background-color: rgba(37, 99, 235, 0.18);
}
QHeaderView::up-arrow {
image: url(:/icons/chevron-up.svg);
}
QHeaderView::down-arrow {
image: url(:/icons/chevron-down.svg);
}
QTableCornerButton::section {
background-color: #f1f5f9;
border: none;
}
7.3 QHeaderView- 支持
::up-arrow、::down-arrow(排序指示)、:checked(列选择)。 QHeaderView::section 可设置 margin 控制勾选框位置。
8. 容器与结构控件8.1 QGroupBox- 子控件:
::title、::indicator(用于可折叠组) - 常用属性:
title 位置(subcontrol-position: top left;)
| 选择器/属性 | 功能说明 | 示例 |
|---|
QGroupBox | 主容器 | border-radius: 10px; padding: 16px 20px 12px; | QGroupBox:hover | 悬停提示 | border-color: rgba(37,99,235,0.35); | QGroupBox[flat="true"] | 扁平组(无边框) | border: none; margin-top: 0; | QGroupBox::title | 标题文本区域 | padding: 0 12px; subcontrol-position: top left; | QGroupBox::title:checked | 勾选标题 | color: #2563eb; | QGroupBox[checkable="true"]::indicator | 勾选框尺寸与样式 | width: 16px; height: 16px; | QGroupBox::indicator:checked | 勾选后状态 | image: url(:/icons/check.svg); | QGroupBox::indicator:hover | 勾选悬停 | border-color: #2563eb; | QGroupBox::indicator:disabled | 禁用勾选 | background-color: rgba(148,163,184,0.25); | QGroupBox > QWidget | 内部直接子控件 | 可额外设置 margin-top 以避免标题覆盖 |
QGroupBox {
border: 1px solid rgba(148, 163, 184, 0.35);
border-radius: 10px;
margin-top: 16px;
padding: 16px 20px 12px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 12px;
font-weight: 600;
color: #1f2937;
background-color: #f8fafc;
border-radius: 12px;
}
QGroupBox[checkable="true"]::indicator {
width: 16px;
height: 16px;
}
QGroupBox[checkable="true"]::indicator:checked {
border: 1px solid #2563eb;
background-color: #2563eb;
image: url(:/icons/check-small.svg);
}
QGroupBox[checkable="true"]::indicator:hover {
border-color: #2563eb;
}
8.2 QFrame- 用于分割线、卡片容器。
- 常见
frameShape:HLine、VLine,可通过 QSS 设置 border、background-color。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QFrame | 基础外观 | border: 1px solid rgba(148,163,184,0.35); | QFrame[frameShape="NoFrame"] | 无边框容器 | border: none; | QFrame[frameShape="Box"] | 盒式边框 | border: 1px solid rgba(15,23,42,0.12); | QFrame[frameShape="StyledPanel"] | 面板风格 | border-radius: 12px; background: #ffffff; | QFrame[frameShape="HLine"] | 水平分割线 | max-height: 1px; background: rgba(...); | QFrame[frameShape="VLine"] | 垂直分割线 | max-width: 1px; background: rgba(...); | QFrame[frameShadow="Raised"] | 凸起阴影(代码控制) | 需配合 setFrameShadow 使用 | qproperty-lineWidth | 边框宽度 | frame->setLineWidth(2); |
8.3 QTabWidget- 组成:
::pane(内容区域)、::tab-bar、QTabBar::tab、QTabBar::close-button - 伪状态:
:selected、:hover、:!selected、:first、:last
| 选择器/属性 | 功能说明 | 示例 |
|---|
QTabWidget::pane | 内容区域外框 | border: 1px solid rgba(148,163,184,0.45); | QTabWidget::tab-bar | 标签条容器 | left: 0px; alignment: center; | QTabBar::tab | 普通标签 | padding: 8px 18px; border-top-left-radius: 8px; | QTabBar::tab:selected | 选中标签 | background: #ffffff; border-bottom-color: transparent; | QTabBar::tab:!selected:hover | 未选中悬停 | color: #2563eb; | QTabBar::tab:first / ::tab:last | 首末标签 | margin-left/right: 0; | QTabBar::tab:disabled | 禁用标签 | color: rgba(51,65,85,0.45); | QTabBar::close-button | 关闭按钮 | subcontrol-position: right; image: url(:/icons/close.svg); | QTabBar::close-button:hover | 关闭按钮悬停 | background: rgba(248,113,113,0.18); | QTabBar::tear | 分离器样式(可拖离) | image: none;(隐藏默认撕裂条) | QTabWidget::pane:top / :bottom | 不同位置的 pane 边界修正 | border-top: none; 等 | qproperty-documentMode | 文档模式(代码配置) | tabWidget->setDocumentMode(true); |
QTabWidget::pane {
border: 1px solid rgba(148, 163, 184, 0.45);
border-radius: 10px;
background-color: #ffffff;
}
QTabBar::tab {
padding: 8px 18px;
margin-right: 4px;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
background-color: rgba(148, 163, 184, 0.12);
color: #334155;
}
QTabBar::tab:selected {
background-color: #ffffff;
color: #2563eb;
border: 1px solid rgba(148, 163, 184, 0.45);
border-bottom-color: transparent;
}
QTabBar::tab:!selected:hover {
color: #2563eb;
}
QTabWidget[documentMode="true"]::pane {
border: none;
}
QTabBar::close-button {
subcontrol-position: right;
image: url(:/icons/close.svg);
margin-left: 8px;
}
QTabBar::close-button:hover {
background-color: rgba(248, 113, 113, 0.18);
border-radius: 4px;
}
8.4 QScrollArea- 主要通过内部
viewport 和滚动条自定义。 - 在 QSS 中使用
QScrollArea > QWidget 选择器可针对内容区设置背景。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QScrollArea | 外框/边界 | border: none; background: transparent; | QScrollArea > QWidget > QWidget | 实际内容容器 | background: #ffffff; | QScrollArea QWidget | 子控件通用设置 | border: none; | QScrollArea::corner | 滚动条交叉区域 | background: rgba(148,163,184,0.12); | QScrollArea[frameShape="NoFrame"] | 取消边框 | border: none; | QScrollArea[widgetResizable="true"] | 自动拉伸内容(代码) | scroll->setWidgetResizable(true); | QScrollArea:disabled | 禁用态 | background: rgba(148,163,184,0.08); | qproperty-alignment | 内容对齐(代码配置) | scroll->setAlignment(Qt::AlignCenter); |
QScrollArea {
border: none;
background: transparent;
}
QScrollArea > QWidget > QWidget {
background: transparent;
}
QScrollArea QWidget {
border: none;
}
QScrollArea::corner {
background-color: rgba(148, 163, 184, 0.12);
}
9. 菜单、工具栏与状态栏9.1 QMenuBar- 子控件:
::item - 伪状态:
:selected、:pressed
| 选择器/属性 | 功能说明 | 示例 |
|---|
QMenuBar | 主体背景 | background-color: #ffffff; | QMenuBar::item | 菜单项 | padding: 6px 12px; border-radius: 6px; | QMenuBar::item:selected | 悬停/激活项 | background-color: rgba(37,99,235,0.12); | QMenuBar::item:pressed | 按压态 | background-color: rgba(37,99,235,0.18); | QMenuBar::item:disabled | 禁用菜单项 | color: rgba(51,65,85,0.45); | QMenuBar::separator | 分割线(Qt 6+) | width: 1px; background: rgba(148,163,184,0.35); | QMenuBar QMenu | 下拉菜单 | 继承 9.2 节设置 | qproperty-nativeMenuBar | 是否使用原生菜单 | menuBar->setNativeMenuBar(false); |
QMenuBar {
background-color: #ffffff;
border-bottom: 1px solid rgba(148, 163, 184, 0.28);
}
QMenuBar::item {
padding: 6px 12px;
margin: 0 4px;
border-radius: 6px;
}
QMenuBar::item:selected {
background-color: rgba(37, 99, 235, 0.12);
color: #2563eb;
}
QMenuBar::item:pressed {
background-color: rgba(37, 99, 235, 0.18);
}
9.2 QMenu- 子控件:
::item、::separator、::indicator(复选/单选菜单项)、::icon - 提示:可使用
menu->setWindowFlags(menu->windowFlags() | Qt::FramelessWindowHint); + QSS 自定义阴影。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QMenu | 菜单面板 | border-radius: 10px; padding: 6px 0; | QMenu::item | 菜单项 | padding: 6px 18px; border-radius: 6px; | QMenu::item:selected | 悬停/选中项 | background-color: rgba(37,99,235,0.12); | QMenu::item:disabled | 禁用项 | color: rgba(51,65,85,0.45); | QMenu::separator | 分割线 | height: 1px; background: rgba(148,163,184,0.35); | QMenu::indicator | 复选/单选标记 | width: 14px; image: url(:/icons/check.svg); | QMenu::icon | 左侧图标区域 | padding-right: 8px; | QMenu[tearoffEnabled="true"] | 撕裂菜单(Qt 支持) | menu->setTearOffEnabled(true); | QMenu::right-arrow | 子菜单箭头 | image: url(:/icons/chevron-right.svg); | QMenu::item:checked | 勾选状态 | background-color: rgba(37,99,235,0.18); |
QMenu {
background-color: #ffffff;
border: 1px solid rgba(15, 23, 42, 0.12);
border-radius: 10px;
padding: 6px 0;
}
QMenu::item {
padding: 6px 18px;
border-radius: 6px;
}
QMenu::item:selected {
background-color: rgba(37, 99, 235, 0.12);
color: #2563eb;
}
QMenu::separator {
height: 1px;
margin: 6px 0;
background-color: rgba(148, 163, 184, 0.35);
}
QMenu::indicator:checked {
image: url(:/icons/check.svg);
}
QMenu::right-arrow {
image: url(:/icons/chevron-right.svg);
}
QMenu::icon {
padding-right: 8px;
}
9.3 QToolBar- 内部通常使用
QToolButton,可针对 QToolBar 设置背景、边框,并定制 ::separator。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QToolBar | 工具栏主体 | background-color: rgba(248,250,252,0.9); | QToolBar::separator | 分隔线 | width: 1px; background-color: rgba(...); | QToolBar::handle | 拖动手柄 | image: url(:/icons/drag-dots.svg); | QToolBar::content(Qt 6+) | 内容区域 | padding: 0 6px; | QToolBar[floatable="true"] | 浮动工具栏 | border: 1px solid rgba(15,23,42,0.15); | QToolBar[orientation="Qt::Vertical"] | 垂直工具栏 | min-width: 40px; | QToolBar QWidget | 内部按钮统一 | 配套 1.3 节 QToolButton | qproperty-movable | 是否可拖拽(代码配置) | toolBar->setMovable(true); |
QToolBar {
background-color: rgba(248, 250, 252, 0.9);
border-bottom: 1px solid rgba(148, 163, 184, 0.28);
spacing: 6px;
padding: 6px 8px;
}
QToolBar::separator {
width: 1px;
background-color: rgba(148, 163, 184, 0.45);
margin: 4px 10px;
}
QToolBar::handle {
image: url(:/icons/drag-dots.svg);
margin: 0 6px;
}
9.4 QStatusBar- 子控件:
::item - 可设置固定高度、背景色,使用
addPermanentWidget() 的控件需要单独样式。
| 选择器/属性 | 功能说明 | 示例 |
|---|
QStatusBar | 状态栏主体 | background-color: #f1f5f9; border-top: 1px solid rgba(...); | QStatusBar::item | 临时消息项 | margin: 0 8px; | QStatusBar QLabel | 文本控件 | color: #475569; | QStatusBar QWidget[permanent="true"] | 常驻控件(动态属性) | 在代码中为常驻控件设置属性后可单独匹配 | QStatusBar::item:message(Qt 6+) | 默认消息标签 | font-weight: 500; | qproperty-sizeGripEnabled | 是否显示尺寸拖拽控件 | statusBar->setSizeGripEnabled(false); |
QStatusBar {
background-color: #f1f5f9;
border-top: 1px solid rgba(148, 163, 184, 0.28);
font-size: 12px;
}
QStatusBar::item {
border: none;
margin: 0 8px;
}
QStatusBar QLabel {
color: #475569;
}
QStatusBar QLabel[permanent="true"] {
margin-left: 16px;
font-weight: 500;
}
QStatusBar QWidget#qt_sizegrip {
width: 12px;
height: 12px;
background: transparent;
}
10. 对话框、提示与辅助控件10.1 QDialog / QMessageBoxQDialog 本身可通过 QDialog 选择器设置背景、圆角、阴影(结合 WA_TranslucentBackground)。QMessageBox 子控件:::label、::icon、::button
| 选择器/属性 | 功能说明 | 示例 |
|---|
QDialog | 对话框主体 | border-radius: 14px; background: #ffffff; | QDialog[modal="true"] | 模态对话框 | border: 1px solid rgba(15,23,42,0.12); | QMessageBox | 消息框主体 | padding: 18px 24px; border-radius: 12px; | QMessageBox QLabel | 文本 | font-size: 14px; color: #0f172a; | QMessageBox QLabel#qt_msgbox_label | 主信息标签 | font-weight: 500; | QMessageBox QLabel#qt_msgboxex_icon_label | 图标 | min-width: 32px; | QMessageBox QPushButton | 按钮 | min-width: 88px; border-radius: 8px; | QMessageBox QPushButton:hover | 按钮悬停 | background-color: rgba(37,99,235,0.12); | QMessageBox QPushButton:default | 默认按钮 | background-color: #2563eb; color: #ffffff; | QMessageBox QPushButton:disabled | 禁用按钮 | color: rgba(71,85,105,0.45); | qproperty-modal | 模态开关(代码设置) | dialog->setModal(true); | qproperty-windowTitle | 窗口标题(代码设置) | dialog->setWindowTitle("提示"); |
QDialog {
background-color: #ffffff;
border-radius: 14px;
border: 1px solid rgba(15, 23, 42, 0.08);
}
QMessageBox {
background-color: #ffffff;
border-radius: 12px;
padding: 18px 24px;
}
QMessageBox QLabel {
color: #0f172a;
font-size: 14px;
}
QMessageBox QPushButton {
min-width: 88px;
padding: 6px 14px;
border-radius: 8px;
border: 1px solid rgba(148, 163, 184, 0.45);
}
QMessageBox QPushButton:hover {
background-color: rgba(37, 99, 235, 0.1);
}
QMessageBox QPushButton:default {
background-color: #2563eb;
border-color: #2563eb;
color: #ffffff;
}
QMessageBox QPushButton:disabled {
background-color: rgba(148, 163, 184, 0.12);
border-color: rgba(148, 163, 184, 0.12);
color: rgba(71, 85, 105, 0.45);
}
10.2 QDialogButtonBox- 聚合多个按钮,根据
ButtonRole 自动布局。 - 可通过
QDialogButtonBox QPushButton[role="AcceptRole"] 匹配按钮角色。
| 选择器/属性 | 功能说明 | 示例 | |
|---|
QDialogButtonBox | 按钮盒容器 | padding-top: 12px; background: transparent; | | QDialogButtonBox QPushButton[role="AcceptRole"] | 接受按钮 | background: #16a34a; color: #ffffff; | | QDialogButtonBox QPushButton[role="RejectRole"] | 拒绝按钮 | background: #ef4444; color: #ffffff; | | QDialogButtonBox QPushButton[role="DestructiveRole"] | 危险操作 | background: #dc2626; | | QDialogButtonBox QPushButton[role="HelpRole"] | 帮助按钮 | background: transparent; color: #2563eb; | | QDialogButtonBox QPushButton:default | 默认按钮 | border: 1px solid #2563eb; | | qproperty-centerButtons(Qt 6+) | 是否居中按钮 | box->setCenterButtons(true); | | qproperty-standardButtons | 标准按钮集合(代码) | `box->setStandardButtons(QDialogButtonBox::Ok | …)` |
QDialogButtonBox {
button-layout: 1;
background: transparent;
padding-top: 12px;
}
QDialogButtonBox QPushButton[role="AcceptRole"] {
background-color: #16a34a;
color: #ffffff;
}
QDialogButtonBox QPushButton[role="RejectRole"] {
background-color: #ef4444;
color: #ffffff;
}
QDialogButtonBox QPushButton[role="HelpRole"] {
background: transparent;
border: 1px solid rgba(37, 99, 235, 0.4);
color: #2563eb;
}
10.3 QToolTip / QWhatsThis| 选择器/属性 | 功能说明 | 示例 |
|---|
QToolTip | 工具提示主体 | background-color: rgba(15,23,42,0.85); | QToolTip + font | 提示字体 | font: 400 12px "Microsoft YaHei"; | QWhatsThis | 帮助提示窗口 | padding: 10px; border-radius: 8px; | QWhatsThis QTextBrowser | 帮助内容区 | font-size: 13px; | qproperty-toolTipDuration | 显示时长 | widget->setToolTipDuration(3000); |
QToolTip {
background-color: rgba(15, 23, 42, 0.85);
color: #f8fafc;
border: 1px solid rgba(15, 23, 42, 0.45);
border-radius: 6px;
padding: 6px 8px;
font: 400 12px "Microsoft YaHei";
}
10.4 QStatusTipEvent- 状态栏提示可通过
QStatusBar 样式覆盖;若需要自定义浮层,可结合自定义 QWidget。
附录 A:常用选择器速查| 控件 | 关键选择器 | 说明 |
|---|
QPushButton | :default、::menu-indicator | 主按钮、菜单按钮 | QCheckBox | ::indicator、:indeterminate | 复选框状态 | QComboBox | ::drop-down、QAbstractItemView | 下拉按钮与弹出列表 | QSlider | ::groove、::handle | 滑块轨道与句柄 | QProgressBar | ::chunk | 进度条填充 | QTableView | QHeaderView::section | 表头 | QTabWidget | QTabBar::tab、::pane | 标签页 | QMenu | ::item、::separator | 菜单项、分隔线 |
附录 B:调试建议- 临时描边法:给
* { outline: 1px solid rgba(255,0,0,0.35); } 便于识别子控件。 - 分层加载:按模块拆分
.qss,最终汇总,避免单文件过大。 - 跨平台校验:在目标平台(Win/macOS/Linux)逐个验证控件是否遵循样式,必要时使用
QProxyStyle 修正。 - 资源管理:将常用 SVG/PNG 放入 Qt Resource(.qrc)统一管理,便于
url(:/res) 调用。 - 版本判断:对于 Qt 6 特性(如
::icon、opacity 属性),使用 #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 条件编译保证兼容。
通过以上控件特性指南,可在通用属性主题的基础上,为不同交互控件快速制定一致又细腻的视觉规范。后续可进一步补充团队自定义控件或第三方控件的
QSS 入口,以形成完整的 UI 设计体系文档。
|