Qt Style Sheets Reference

Reference

Reference

Home · All Classes · Modules · QSS HELP · QSS 案例 · VER007 HOME

Qt 样式表 例子

样式表的使用

自定义 前景和背景颜色

让我们通过设置黄色是所有的背景颜色开始 QLineEdit 某个应用程序中。 这可以实现如下.

  1. qApp->setStyleSheet("QLineEdit { background-color: yellow }");

如果我们希望属性只适用于QLineEdits中的一个特定的对话框的子项(或孙子或重孙子)

  1. myDialog->setStyleSheet("QLineEdit { background-color: yellow }");

如果我们希望属性只适用于一个特定的QLineEdit, 我们可以给它使用名称 QObject::setObjectName() 并使用一个ID选择器来引用它:

  1. myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");

或者,我们可以设置background-color 的直接财产QLineEdit, 省略了选择

  1. nameEdit->setStyleSheet("background-color: yellow");

为了确保良好的对比,我们还应该指定一个合适的颜色的文本

  1. nameEdit->setStyleSheet("color: blue; background-color: yellow");

这可能是更改用于选定的文本以及色彩的一个好主意:

  1. nameEdit->setStyleSheet("color: blue;"
  2. "background-color: yellow;"
  3. "selection-color: yellow;"
  4. "selection-background-color: blue;");

使用自定义动态属性

还有,我们需要提出一个具有强制性的字段的表格很多情况下。 以指示该字段是强制性的,一次有效(虽然美观可疑)解决方案是使用黄色作为背景色为这些字段的用户。 事实证明,这是非常容易使用Qt样式表来实现。 首先,我们可以使用下面的应用程序范围内的样式表

  1. *[mandatoryField="true"] { background-color: yellow }

这意味着,每个部件的mandatoryField Qt的属性设置为true,将有一个黄色的背景。

然后,对于每个必填字段部件,我们可以简单地创建一个mandatoryField动态属性,并将其设置为true。 例如:

  1. [QLineEdit](qlineedit.html) *nameEdit = new [QLineEdit](qlineedit.html)(this);
  2. nameEdit->setProperty("mandatoryField", true);
  3. [QLineEdit](qlineedit.html) *emailEdit = new [QLineEdit](qlineedit.html)(this);
  4. emailEdit->setProperty("mandatoryField", true);
  5. [QSpinBox](qspinbox.html) *ageSpinBox = new [QSpinBox](qspinbox.html)(this);
  6. ageSpinBox->setProperty("mandatoryField", true);

自定义QPushButton使用盒模型

这一次,我们将展示如何创建一个红色QPushButton. 这QPushButton 大概会被连接到一个极具破坏性的一段代码。

首先,我们要使用这个样式表:

  1. [QPushButton](qpushbutton.html)#evilButton { background-color: red }

但是,结果是一个没有边界没有平整:

A flat red button

发生了什么事呢:

  • 我们已经提出,不能单独使用本地样式来满足的请求(例如,Windows XP主题引擎不会让我们指定按钮的背景颜色)。
  • 因此,按钮被使用样式表呈现。
  • 我们没有指定任何值border-width and border-style, 所以默认情况下,我们得到的风格0像素宽的边框none 。

让我们通过指定边界改善情况:

  1. [QPushButton](qpushbutton.html)#evilButton {
  2. background-color: red;
  3. border-style: outset;
  4. border-width: 2px;
  5. border-color: beige;
  6. }

A red button with a beige border

事情看起来已经好了很多。 但是,按钮看起来有点局促。 让我们来指定边框,并使用文字之间的间距一些 padding. 此外,我们将执行的最小宽度,圆形边角,并指定一个更大的字体,使按钮看起来更好:

  1. [QPushButton](qpushbutton.html)#evilButton {
  2. background-color: red;
  3. border-style: outset;
  4. border-width: 2px;
  5. border-radius: 10px;
  6. border-color: beige;
  7. font: bold 14px;
  8. min-width: 10em;
  9. padding: 6px;
  10. }

A red button with a round beige border and big, bold text

剩下唯一的问题是,当我们按下按钮没有反应。 我们可以通过指定一个稍微不同的背景色修复它并使用不同的边框样式。

  1. [QPushButton](qpushbutton.html)#evilButton {
  2. background-color: red;
  3. border-style: outset;
  4. border-width: 2px;
  5. border-radius: 10px;
  6. border-color: beige;
  7. font: bold 14px;
  8. min-width: 10em;
  9. padding: 6px;
  10. }
  11. [QPushButton](qpushbutton.html)#evilButton:pressed {
  12. background-color: rgb(224, 0, 0);
  13. border-style: inset;
  14. }

自定义QPushButton的菜单指示器分控

子控件授予访问小窗口的子元素。 例如,一个 QPushButton 与菜单相关联(使用 QPushButton::setMenu()) 有一个菜单指示符。 让我们定制的红色按钮菜单指标:

  1. [QPushButton](qpushbutton.html)#evilButton::menu-indicator {
  2. image: url(myindicator.png);
  3. }

默认情况下,菜单指示器位于填充矩形的右下角。 我们可以通过指定改变这个 subcontrol-position 和 subcontrol-origin to以不同的锚定指标。 我们也可以用top and left由几个像素来移动指针。 例如:

  1. [QPushButton](qpushbutton.html)::menu-indicator {
  2. image: url(myindicator.png);
  3. subcontrol-position: right center;
  4. subcontrol-origin: padding;
  5. left: -2px;
  6. }

这种定位的myindicator.png到的中间偏右 QPushButton‘s padding 矩形 (见 subcontrol-origin 获取更多信息).

复杂的选择示例

由于红色似乎是我们最喜欢的颜色,让我们在文中 QLineEdit 通过设置以下应用程序范围的样式表红:

  1. [QLineEdit](qlineedit.html) { color: red }

但是,我们想给的视觉指示一个 QLineEdit 是只读使其显示为灰色:

  1. [QLineEdit](qlineedit.html) { color: red }
  2. [QLineEdit](qlineedit.html)[readOnly="true"] { color: gray }

在某些时候,我们的设计团队配备了所有的要求 QLineEdit 在登记表(使用 object name registrationDialog) 为棕色

  1. [QLineEdit](qlineedit.html) { color: red }
  2. [QLineEdit](qlineedit.html)[readOnly="true"] { color: gray }
  3. #registrationDialog QLineEdit { color: brown }

一些UI设计的会议之后,我们决定,我们所有的 QDialog 应该具有棕色QLineEdit里:

  1. [QLineEdit](qlineedit.html) { color: red }
  2. [QLineEdit](qlineedit.html)[readOnly="true"] { color: gray }
  3. [QDialog]($docs-qdialog.html) [QLineEdit](qlineedit.html) { color: brown }

自定义特定的部件

本节提供了示例来使用自定义样式表的特定部件。

自定义 QAbstractScrollArea

任何背景 QAbstractScrollArea (项目的意见, QTextEdit and QTextBrowser) 可以使用背景属性进行设置。 例如,要设置一个背景图像,与滚动条滚动:

  1. [QTextEdit](qtextedit.html), [QListView](qlistview.html) {
  2. background-color: white;
  3. background-image: url(draft.png);
  4. background-attachment: scroll;
  5. }

如果该背景图像是被固定的视区:

  1. [QTextEdit](qtextedit.html), [QListView](qlistview.html) {
  2. background-color: white;
  3. background-image: url(draft.png);
  4. background-attachment: fixed;
  5. }

自定义 QCheckBox

一个定型 QCheckBox is almost indentical to styling a QRadioButton. 的主要区别在于,一个三态 QCheckBox 具有不确定状态。

  1. [QCheckBox]($docs-qcheckbox.html) {
  2. spacing: 5px;
  3. }
  4. [QCheckBox]($docs-qcheckbox.html)::indicator {
  5. width: 13px;
  6. height: 13px;
  7. }
  8. [QCheckBox]($docs-qcheckbox.html)::indicator:unchecked {
  9. image: url(:/../img/checkbox_unchecked.png);
  10. }
  11. [QCheckBox]($docs-qcheckbox.html)::indicator:unchecked:hover {
  12. image: url(:/../img/checkbox_unchecked_hover.png);
  13. }
  14. [QCheckBox]($docs-qcheckbox.html)::indicator:unchecked:pressed {
  15. image: url(:/../img/checkbox_unchecked_pressed.png);
  16. }
  17. [QCheckBox]($docs-qcheckbox.html)::indicator:checked {
  18. image: url(:/../img/checkbox_checked.png);
  19. }
  20. [QCheckBox]($docs-qcheckbox.html)::indicator:checked:hover {
  21. image: url(:/../img/checkbox_checked_hover.png);
  22. }
  23. [QCheckBox]($docs-qcheckbox.html)::indicator:checked:pressed {
  24. image: url(:/../img/checkbox_checked_pressed.png);
  25. }
  26. [QCheckBox]($docs-qcheckbox.html)::indicator:indeterminate:hover {
  27. image: url(:/../img/checkbox_indeterminate_hover.png);
  28. }
  29. [QCheckBox]($docs-qcheckbox.html)::indicator:indeterminate:pressed {
  30. image: url(:/../img/checkbox_indeterminate_pressed.png);
  31. }

自定义 QComboBox

我们将看看那里下拉一个按钮一个例子 QComboBox 出现“合并”与组合框框架

  1. [QComboBox](qcombobox.html) {
  2. border: 1px solid gray;
  3. border-radius: 3px;
  4. padding: 1px 18px 1px 3px;
  5. min-width: 6em;
  6. }
  7. [QComboBox](qcombobox.html):editable {
  8. background: white;
  9. }
  10. [QComboBox](qcombobox.html):!editable, [QComboBox](qcombobox.html)::drop-down:editable {
  11. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  12. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  13. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  14. }
  15. /* QComboBox gets the "on" state when the popup is open */
  16. [QComboBox](qcombobox.html):!editable:on, [QComboBox](qcombobox.html)::drop-down:editable:on {
  17. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  18. stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,
  19. stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);
  20. }
  21. [QComboBox](qcombobox.html):on { /* shift the text when the popup opens */
  22. padding-top: 3px;
  23. padding-left: 4px;
  24. }
  25. [QComboBox](qcombobox.html)::drop-down {
  26. subcontrol-origin: padding;
  27. subcontrol-position: top right;
  28. width: 15px;
  29. border-left-width: 1px;
  30. border-left-color: darkgray;
  31. border-left-style: solid; /* just a single line */
  32. border-top-right-radius: 3px; /* same radius as the QComboBox */
  33. border-bottom-right-radius: 3px;
  34. }
  35. [QComboBox](qcombobox.html)::down-arrow {
  36. image: url(/usr/share/icons/crystalsvg/16x16/actions/1downarrow.png);
  37. }
  38. [QComboBox](qcombobox.html)::down-arrow:on { /* shift the arrow when popup is open */
  39. top: 1px;
  40. left: 1px;
  41. }

该的弹出 QComboBox is a QAbstractItemView 并使用后代选择的样式:

  1. [QComboBox](qcombobox.html) [QAbstractItemView]($docs-qabstractitemview.html) {
  2. border: 2px solid darkgray;
  3. selection-background-color: lightgray;
  4. }

自定义 QDockWidget

标题栏和的按钮 QDockWidget 可自定义如下:

  1. [QDockWidget]($docs-qdockwidget.html) {
  2. border: 1px solid lightgray;
  3. titlebar-close-icon: url(close.png);
  4. titlebar-normal-icon: url(undock.png);
  5. }
  6. [QDockWidget]($docs-qdockwidget.html)::title {
  7. text-align: left; /* align the text to the left */
  8. background: lightgray;
  9. padding-left: 5px;
  10. }
  11. [QDockWidget]($docs-qdockwidget.html)::close-button, [QDockWidget]($docs-qdockwidget.html)::float-button {
  12. border: 1px solid transparent;
  13. background: darkgray;
  14. padding: 0px;
  15. }
  16. [QDockWidget]($docs-qdockwidget.html)::close-button:hover, [QDockWidget]($docs-qdockwidget.html)::float-button:hover {
  17. background: gray;
  18. }
  19. [QDockWidget]($docs-qdockwidget.html)::close-button:pressed, [QDockWidget]($docs-qdockwidget.html)::float-button:pressed {
  20. padding: 1px -1px -1px 1px;
  21. }

如果一个人希望Dock Widget部件按钮移动到左侧,下面的样式表,可以用:

  1. [QDockWidget]($docs-qdockwidget.html) {
  2. border: 1px solid lightgray;
  3. titlebar-close-icon: url(close.png);
  4. titlebar-normal-icon: url(float.png);
  5. }
  6. [QDockWidget]($docs-qdockwidget.html)::title {
  7. text-align: left;
  8. background: lightgray;
  9. padding-left: 35px;
  10. }
  11. [QDockWidget]($docs-qdockwidget.html)::close-button, [QDockWidget]($docs-qdockwidget.html)::float-button {
  12. background: darkgray;
  13. padding: 0px;
  14. icon-size: 14px; /* maximum icon size */
  15. }
  16. [QDockWidget]($docs-qdockwidget.html)::close-button:hover, [QDockWidget]($docs-qdockwidget.html)::float-button:hover {
  17. background: gray;
  18. }
  19. [QDockWidget]($docs-qdockwidget.html)::close-button:pressed, [QDockWidget]($docs-qdockwidget.html)::float-button:pressed {
  20. padding: 1px -1px -1px 1px;
  21. }
  22. [QDockWidget]($docs-qdockwidget.html)::close-button {
  23. subcontrol-position: top left;
  24. subcontrol-origin: margin;
  25. position: absolute;
  26. top: 0px; left: 0px; bottom: 0px;
  27. width: 14px;
  28. }
  29. [QDockWidget]($docs-qdockwidget.html)::float-button {
  30. subcontrol-position: top left;
  31. subcontrol-origin: margin;
  32. position: absolute;
  33. top: 0px; left: 16px; bottom: 0px;
  34. width: 14px;
  35. }

Note: 要自定义分离器的(调整句柄) QDockWidget, 使用的QMainWindow ::分隔符。

自定义 QFrame

A QFrame 是使用样式 The Box Model.

  1. [QFrame](qframe.html), [QLabel](qlabel.html), [QToolTip](qtooltip.html) {
  2. border: 2px solid green;
  3. border-radius: 4px;
  4. padding: 2px;
  5. background-image: url(../img/welcome.png);
  6. }

自定义 QGroupBox

让我们看一下移动为例 QGroupBox‘里 的标题为中心。

  1. [QGroupBox](qgroupbox.html) {
  2. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #E0E0E0, stop: 1 #FFFFFF);
  4. border: 2px solid gray;
  5. border-radius: 5px;
  6. margin-top: 1ex; /* leave space at the top for the title */
  7. }
  8. [QGroupBox](qgroupbox.html)::title {
  9. subcontrol-origin: margin;
  10. subcontrol-position: top center; /* position at the top center */
  11. padding: 0 3px;
  12. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  13. stop: 0 #FFOECE, stop: 1 #FFFFFF);
  14. }

对于一个可检查的 QGroupBox, 使用 {#indicator-sub}{indicator} 子控件和风格它酷似一个 QCheckBox (i.e)

  1. [QGroupBox](qgroupbox.html)::indicator {
  2. width: 13px;
  3. height: 13px;
  4. }
  5. [QGroupBox](qgroupbox.html)::indicator:unchecked {
  6. image: url(:/../img/checkbox_unchecked.png);
  7. }
  8. /* proceed with styling just like QCheckBox */

自定义 QHeaderView

QHeaderView 定制如下:

  1. [QHeaderView]($docs-qheaderview.html)::section {
  2. background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
  3. stop:0 #616161, stop: 0.5 #505050,
  4. stop: 0.6 #434343, stop:1 #656565);
  5. color: white;
  6. padding-left: 4px;
  7. border: 1px solid #6c6c6c;
  8. }
  9. [QHeaderView]($docs-qheaderview.html)::section:checked
  10. {
  11. background-color: red;
  12. }
  13. /* style the sort indicator */
  14. [QHeaderView]($docs-qheaderview.html)::down-arrow {
  15. image: url(down_arrow.png);
  16. }
  17. [QHeaderView]($docs-qheaderview.html)::up-arrow {
  18. image: url(up_arrow.png);
  19. }

自定义 QLineEdit

这个框架 QLineEdit 正在使用的样式 The Box Model. 要创建一个圆角线的编辑,我们可以设置:

  1. [QLineEdit](qlineedit.html) {
  2. border: 2px solid gray;
  3. border-radius: 10px;
  4. padding: 0 8px;
  5. background: yellow;
  6. selection-background-color: darkgray;
  7. }

线编辑的密码字符有 QLineEdit::Password 回波模式可以使用设置:

  1. [QLineEdit](qlineedit.html)[echoMode="2"] {
  2. lineedit-password-character: 9679;
  3. }

一个只读的背景 QLineEdit 可被修改如下:

  1. [QLineEdit](qlineedit.html):read-only {
  2. background: lightblue;

自定义 QListView

交替行的背景颜色可以用下面的样式表进行定制

  1. [QListView](qlistview.html) {
  2. alternate-background-color: yellow;
  3. }

为了提供当你将鼠标悬停在项的特殊背景下,我们可以使用 ::item 子控件。 例如,

  1. [QListView](qlistview.html) {
  2. show-decoration-selected: 1; /* make the selection span the entire width of the view */
  3. }
  4. [QListView](qlistview.html)::item:alternate {
  5. background: #EEEEEE;
  6. }
  7. [QListView](qlistview.html)::item:selected {
  8. border: 1px solid #6a6ea9;
  9. }
  10. [QListView](qlistview.html)::item:selected:!active {
  11. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  12. stop: 0 #ABAFE5, stop: 1 #8588B2);
  13. }
  14. [QListView](qlistview.html)::item:selected:active {
  15. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  16. stop: 0 #6a6ea9, stop: 1 #888dd9);
  17. }
  18. [QListView](qlistview.html)::item:hover {
  19. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  20. stop: 0 #FAFBFE, stop: 1 #DCDEF1);
  21. }

自定义 QMainWindow

一个分离器的 QMainWindow 可以样式如下

  1. [QMainWindow](qmainwindow.html)::separator {
  2. background: yellow;
  3. width: 10px; /* when vertical */
  4. height: 10px; /* when horizontal */
  5. }
  6. [QMainWindow](qmainwindow.html)::separator:hover {
  7. background: red;
  8. }

自定义 QMenu

一个个人项目 QMenu 中使用了’项目’子控件如下样式:

  1. [QMenu]($docs-qmenu.html) {
  2. background-color: #ABABAB; /* sets background of the menu */
  3. border: 1px solid black;
  4. }
  5. [QMenu]($docs-qmenu.html)::item {
  6. /* sets background of menu item. set this to something non-transparent
  7. if you want menu color and menu item color to be different */
  8. background-color: transparent;
  9. }
  10. [QMenu]($docs-qmenu.html)::item:selected { /* when user selects item using mouse or keyboard */
  11. background-color: #654321;
  12. }

对于更高级的定制,按如下方式使用样式表:

  1. [QMenu]($docs-qmenu.html) {
  2. background-color: white;
  3. margin: 2px; /* some spacing around the menu */
  4. }
  5. [QMenu]($docs-qmenu.html)::item {
  6. padding: 2px 25px 2px 20px;
  7. border: 1px solid transparent; /* reserve space for selection border */
  8. }
  9. [QMenu]($docs-qmenu.html)::item:selected {
  10. border-color: darkblue;
  11. background: rgba(100, 100, 100, 150);
  12. }
  13. [QMenu]($docs-qmenu.html)::icon:checked { /* appearance of a 'checked' icon */
  14. background: gray;
  15. border: 1px inset gray;
  16. position: absolute;
  17. top: 1px;
  18. right: 1px;
  19. bottom: 1px;
  20. left: 1px;
  21. }
  22. [QMenu]($docs-qmenu.html)::separator {
  23. height: 2px;
  24. background: lightblue;
  25. margin-left: 10px;
  26. margin-right: 5px;
  27. }
  28. [QMenu]($docs-qmenu.html)::indicator {
  29. width: 13px;
  30. height: 13px;
  31. }
  32. /* non-exclusive indicator = check box style indicator (see QActionGroup::setExclusive) */
  33. [QMenu]($docs-qmenu.html)::indicator:non-exclusive:unchecked {
  34. image: url(:/../img/checkbox_unchecked.png);
  35. }
  36. [QMenu]($docs-qmenu.html)::indicator:non-exclusive:unchecked:selected {
  37. image: url(:/../img/checkbox_unchecked_hover.png);
  38. }
  39. [QMenu]($docs-qmenu.html)::indicator:non-exclusive:checked {
  40. image: url(:/../img/checkbox_checked.png);
  41. }
  42. [QMenu]($docs-qmenu.html)::indicator:non-exclusive:checked:selected {
  43. image: url(:/../img/checkbox_checked_hover.png);
  44. }
  45. /* exclusive indicator = radio button style indicator (see QActionGroup::setExclusive) */
  46. [QMenu]($docs-qmenu.html)::indicator:exclusive:unchecked {
  47. image: url(:/../img/radiobutton_unchecked.png);
  48. }
  49. [QMenu]($docs-qmenu.html)::indicator:exclusive:unchecked:selected {
  50. image: url(:/../img/radiobutton_unchecked_hover.png);
  51. }
  52. [QMenu]($docs-qmenu.html)::indicator:exclusive:checked {
  53. image: url(:/../img/radiobutton_checked.png);
  54. }
  55. [QMenu]($docs-qmenu.html)::indicator:exclusive:checked:selected {
  56. image: url(:/../img/radiobutton_checked_hover.png);
  57. }

自定义 QMenuBar

QMenuBar 的样式如下:

  1. [QMenuBar](qmenubar.html) {
  2. background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
  3. stop:0 lightgray, stop:1 darkgray);
  4. }
  5. [QMenuBar](qmenubar.html)::item {
  6. spacing: 3px; /* spacing between menu bar items */
  7. padding: 1px 4px;
  8. background: transparent;
  9. border-radius: 4px;
  10. }
  11. [QMenuBar](qmenubar.html)::item:selected { /* when selected using mouse or keyboard */
  12. background: #a8a8a8;
  13. }
  14. [QMenuBar](qmenubar.html)::item:pressed {
  15. background: #888888;
  16. }

自定义 QProgressBar

QProgressBar‘s border, chunk, 和 text-align 可以使用样式表来定制。 然而,如果一个属性或副控制被定制,所有其他属性或子控制也必须定做。

Qt 样式表 例子 - 图4

例如,我们改变 border 为灰色和 chunk 蔚蓝。

  1. [QProgressBar](qprogressbar.html) {
  2. border: 2px solid grey;
  3. border-radius: 5px;
  4. }
  5. [QProgressBar](qprogressbar.html)::chunk {
  6. background-color: #05B8CC;
  7. width: 20px;
  8. }

这使得 text-align, 我们通过定位在进度条的中央的文字定义。

  1. [QProgressBar](qprogressbar.html) {
  2. border: 2px solid grey;
  3. border-radius: 5px;
  4. text-align: center;
  5. }

一个 margin 可以包括获得更多的可见块。

Qt 样式表 例子 - 图5

在上面的截图中,我们使用了 margin 的0.5像素。

  1. [QProgressBar](qprogressbar.html)::chunk {
  2. background-color: #CD96CD;
  3. width: 10px;
  4. margin: 0.5px;
  5. }

自定义 QPushButton

一个 QPushButton 的样式如下:

  1. [QPushButton](qpushbutton.html) {
  2. border: 2px solid #8f8f91;
  3. border-radius: 6px;
  4. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  5. stop: 0 #f6f7fa, stop: 1 #dadbde);
  6. min-width: 80px;
  7. }
  8. [QPushButton](qpushbutton.html):pressed {
  9. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  10. stop: 0 #dadbde, stop: 1 #f6f7fa);
  11. }
  12. [QPushButton](qpushbutton.html):flat {
  13. border: none; /* no border for a flat push button */
  14. }
  15. [QPushButton](qpushbutton.html):default {
  16. border-color: navy; /* make the default button prominent */
  17. }

对于 QPushButton 有菜单,使用 ::menu-indicator 子控件。

  1. [QPushButton](qpushbutton.html):open { /* when the button has its menu open */
  2. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #dadbde, stop: 1 #f6f7fa);
  4. }
  5. [QPushButton](qpushbutton.html)::menu-indicator {
  6. image: url(menu_indicator.png);
  7. subcontrol-origin: padding;
  8. subcontrol-position: bottom right;
  9. }
  10. [QPushButton](qpushbutton.html)::menu-indicator:pressed, [QPushButton](qpushbutton.html)::menu-indicator:open {
  11. position: relative;
  12. top: 2px; left: 2px; /* shift the arrow by 2 px */
  13. }

可勾选 QPushButton:checked 伪状态集

自定义 QRadioButton

一个 QRadioButton 可以进行转换

  1. [QRadioButton]($docs-qradiobutton.html)::indicator {
  2. width: 13px;
  3. height: 13px;
  4. }
  5. [QRadioButton]($docs-qradiobutton.html)::indicator::unchecked {
  6. image: url(:/../img/radiobutton_unchecked.png);
  7. }
  8. [QRadioButton]($docs-qradiobutton.html)::indicator:unchecked:hover {
  9. image: url(:/../img/radiobutton_unchecked_hover.png);
  10. }
  11. [QRadioButton]($docs-qradiobutton.html)::indicator:unchecked:pressed {
  12. image: url(:/../img/radiobutton_unchecked_pressed.png);
  13. }
  14. [QRadioButton]($docs-qradiobutton.html)::indicator::checked {
  15. image: url(:/../img/radiobutton_checked.png);
  16. }
  17. [QRadioButton]($docs-qradiobutton.html)::indicator:checked:hover {
  18. image: url(:/../img/radiobutton_checked_hover.png);
  19. }
  20. [QRadioButton]($docs-qradiobutton.html)::indicator:checked:pressed {
  21. image: url(:/../img/radiobutton_checked_pressed.png);
  22. }

自定义 QScrollBar

QScrollBar 可以使用其子控件等来称呼 handle, add-line, sub-line, 依此类推。 请注意,如果一个属性或副控制被定制,所有其他属性或子控制也必须定做。

Qt 样式表 例子 - 图6

滚动条上面已经在风格蓝晶了坚实的灰色边框。

  1. [QScrollBar](qscrollbar.html):horizontal {
  2. border: 2px solid grey;
  3. background: #32CC99;
  4. height: 15px;
  5. margin: 0px 20px 0 20px;
  6. }
  7. [QScrollBar](qscrollbar.html)::handle:horizontal {
  8. background: white;
  9. min-width: 20px;
  10. }
  11. [QScrollBar](qscrollbar.html)::add-line:horizontal {
  12. border: 2px solid grey;
  13. background: #32CC99;
  14. width: 20px;
  15. subcontrol-position: right;
  16. subcontrol-origin: margin;
  17. }
  18. [QScrollBar](qscrollbar.html)::sub-line:horizontal {
  19. border: 2px solid grey;
  20. background: #32CC99;
  21. width: 20px;
  22. subcontrol-position: left;
  23. subcontrol-origin: margin;
  24. }

left-arrow and right-arrow 有一个白色的背景了坚实的灰色边框。 作为替代方案,你也可以嵌入一个箭头的形象。

  1. [QScrollBar](qscrollbar.html):left-arrow:horizontal, [QScrollBar](qscrollbar.html)::right-arrow:horizontal {
  2. border: 2px solid grey;
  3. width: 3px;
  4. height: 3px;
  5. background: white;
  6. }
  7. [QScrollBar](qscrollbar.html)::add-page:horizontal, [QScrollBar](qscrollbar.html)::sub-page:horizontal {
  8. background: none;
  9. }

如果你想在滚动条的滚动按钮被放置在一起(而不是边缘),如Mac OS X上,你可以使用下面的样式表:

  1. [QScrollBar](qscrollbar.html):horizontal {
  2. border: 2px solid green;
  3. background: cyan;
  4. height: 15px;
  5. margin: 0px 40px 0 0px;
  6. }
  7. [QScrollBar](qscrollbar.html)::handle:horizontal {
  8. background: gray;
  9. min-width: 20px;
  10. }
  11. [QScrollBar](qscrollbar.html)::add-line:horizontal {
  12. background: blue;
  13. width: 16px;
  14. subcontrol-position: right;
  15. subcontrol-origin: margin;
  16. border: 2px solid black;
  17. }
  18. [QScrollBar](qscrollbar.html)::sub-line:horizontal {
  19. background: magenta;
  20. width: 16px;
  21. subcontrol-position: top right;
  22. subcontrol-origin: margin;
  23. border: 2px solid black;
  24. position: absolute;
  25. right: 20px;
  26. }
  27. [QScrollBar](qscrollbar.html):left-arrow:horizontal, [QScrollBar](qscrollbar.html)::right-arrow:horizontal {
  28. width: 3px;
  29. height: 3px;
  30. background: pink;
  31. }
  32. [QScrollBar](qscrollbar.html)::add-page:horizontal, [QScrollBar](qscrollbar.html)::sub-page:horizontal {
  33. background: none;
  34. }

使用上述样式表的滚动条是这样的:

Qt 样式表 例子 - 图7

要自定义垂直滚动条使用类似如下的样式表:

  1. [QScrollBar](qscrollbar.html):vertical {
  2. border: 2px solid grey;
  3. background: #32CC99;
  4. width: 15px;
  5. margin: 22px 0 22px 0;
  6. }
  7. [QScrollBar](qscrollbar.html)::handle:vertical {
  8. background: white;
  9. min-height: 20px;
  10. }
  11. [QScrollBar](qscrollbar.html)::add-line:vertical {
  12. border: 2px solid grey;
  13. background: #32CC99;
  14. height: 20px;
  15. subcontrol-position: bottom;
  16. subcontrol-origin: margin;
  17. }
  18. [QScrollBar](qscrollbar.html)::sub-line:vertical {
  19. border: 2px solid grey;
  20. background: #32CC99;
  21. height: 20px;
  22. subcontrol-position: top;
  23. subcontrol-origin: margin;
  24. }
  25. [QScrollBar](qscrollbar.html)::up-arrow:vertical, [QScrollBar](qscrollbar.html)::down-arrow:vertical {
  26. border: 2px solid grey;
  27. width: 3px;
  28. height: 3px;
  29. background: white;
  30. }
  31. [QScrollBar](qscrollbar.html)::add-page:vertical, [QScrollBar](qscrollbar.html)::sub-page:vertical {
  32. background: none;
  33. }

自定义 QSizeGrip

QSizeGrip 常是由刚刚设置的图像风格。

  1. [QSizeGrip]($docs-qsizegrip.html) {
  2. image: url(:/../img/sizegrip.png);
  3. width: 16px;
  4. height: 16px;
  5. }

自定义 QSlider

你可以样式水平滑块如下:

  1. [QSlider](qslider.html)::groove:horizontal {
  2. border: 1px solid #999999;
  3. height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
  4. background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
  5. margin: 2px 0;
  6. }
  7. [QSlider](qslider.html)::handle:horizontal {
  8. background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
  9. border: 1px solid #5c5c5c;
  10. width: 18px;
  11. margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
  12. border-radius: 3px;
  13. }

如果你想把手前后改变滑块部分的颜色,可以使用加载页面和子页面子控件。 例如,对于一个垂直滑块:

  1. [QSlider](qslider.html)::groove:vertical {
  2. background: red;
  3. position: absolute; /* absolutely position 4px from the left and right of the widget. setting margins on the widget should work too... */
  4. left: 4px; right: 4px;
  5. }
  6. [QSlider](qslider.html)::handle:vertical {
  7. height: 10px;
  8. background: green;
  9. margin: 0 -4px; /* expand outside the groove */
  10. }
  11. [QSlider](qslider.html)::add-page:vertical {
  12. background: white;
  13. }
  14. [QSlider](qslider.html)::sub-page:vertical {
  15. background: pink;
  16. }

自定义 QSpinBox

QSpinBox 可完全自定义如下(样式表有评论在线):

  1. [QSpinBox](qspinbox.html) {
  2. padding-right: 15px; /* make room for the arrows */
  3. border-image: url(:/../img/frame.png) 4;
  4. border-width: 3;
  5. }
  6. [QSpinBox](qspinbox.html)::up-button {
  7. subcontrol-origin: border;
  8. subcontrol-position: top right; /* position at the top right corner */
  9. width: 16px; /* 16 + 2*1px border-width = 15px padding + 3px parent border */
  10. border-image: url(:/../img/spinup.png) 1;
  11. border-width: 1px;
  12. }
  13. [QSpinBox](qspinbox.html)::up-button:hover {
  14. border-image: url(:/../img/spinup_hover.png) 1;
  15. }
  16. [QSpinBox](qspinbox.html)::up-button:pressed {
  17. border-image: url(:/../img/spinup_pressed.png) 1;
  18. }
  19. [QSpinBox](qspinbox.html)::up-arrow {
  20. image: url(:/../img/up_arrow.png);
  21. width: 7px;
  22. height: 7px;
  23. }
  24. [QSpinBox](qspinbox.html)::up-arrow:disabled, [QSpinBox](qspinbox.html)::up-arrow:off { /* off state when value is max */
  25. image: url(:/../img/up_arrow_disabled.png);
  26. }
  27. [QSpinBox](qspinbox.html)::down-button {
  28. subcontrol-origin: border;
  29. subcontrol-position: bottom right; /* position at bottom right corner */
  30. width: 16px;
  31. border-image: url(:/../img/spindown.png) 1;
  32. border-width: 1px;
  33. border-top-width: 0;
  34. }
  35. [QSpinBox](qspinbox.html)::down-button:hover {
  36. border-image: url(:/../img/spindown_hover.png) 1;
  37. }
  38. [QSpinBox](qspinbox.html)::down-button:pressed {
  39. border-image: url(:/../img/spindown_pressed.png) 1;
  40. }
  41. [QSpinBox](qspinbox.html)::down-arrow {
  42. image: url(:/../img/down_arrow.png);
  43. width: 7px;
  44. height: 7px;
  45. }
  46. [QSpinBox](qspinbox.html)::down-arrow:disabled,
  47. [QSpinBox](qspinbox.html)::down-arrow:off { /* off state when value in min */
  48. image: url(:/../img/down_arrow_disabled.png);
  49. }

自定义 QSplitter

一个 QSplitter 从派生 QFrame 和因此可以样式像 QFrame. 把手或句柄使用定制 ::handle 子控件.

  1. [QSplitter](qsplitter.html)::handle {
  2. image: url(../img/splitter.png);
  3. }
  4. [QSplitter](qsplitter.html)::handle:horizontal {
  5. width: 2px;
  6. }
  7. [QSplitter](qsplitter.html)::handle:vertical {
  8. height: 2px;
  9. }
  10. [QSplitter](qsplitter.html)::handle:pressed {
  11. image: url(../img/splitter_pressed.png);
  12. }

自定义 QStatusBar

我们可以提供状态栏的背景和状态栏里面的物品边框,如下所示:

  1. [QStatusBar]($docs-qstatusbar.html) {
  2. background: brown;
  3. }
  4. [QStatusBar]($docs-qstatusbar.html)::item {
  5. border: 1px solid red;
  6. border-radius: 3px;
  7. }

需要注意的是已被添加到窗口小部件 QStatusBar 可以使用后代声明的样式(即)

  1. [QStatusBar]($docs-qstatusbar.html) [QLabel](qlabel.html) {
  2. border: 3px solid white;
  3. }

自定义 QTabWidget and QTabBar

Qt 样式表 例子 - 图8

对于上面的截图中,我们需要一个样式表如下:

  1. [QTabWidget](qtabwidget.html)::pane { /* The tab widget frame */
  2. border-top: 2px solid #C2C7CB;
  3. }
  4. [QTabWidget](qtabwidget.html)::tab-bar {
  5. left: 5px; /* move to the right by 5px */
  6. }
  7. /* Style the tab using the tab sub-control. Note that
  8. it reads QTabBar _not_ QTabWidget */
  9. [QTabBar](qtabbar.html)::tab {
  10. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  11. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  12. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  13. border: 2px solid #C4C4C3;
  14. border-bottom-color: #C2C7CB; /* same as the pane color */
  15. border-top-left-radius: 4px;
  16. border-top-right-radius: 4px;
  17. min-width: 8ex;
  18. padding: 2px;
  19. }
  20. [QTabBar](qtabbar.html)::tab:selected, [QTabBar](qtabbar.html)::tab:hover {
  21. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  22. stop: 0 #fafafa, stop: 0.4 #f4f4f4,
  23. stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
  24. }
  25. [QTabBar](qtabbar.html)::tab:selected {
  26. border-color: #9B9B9B;
  27. border-bottom-color: #C2C7CB; /* same as pane color */
  28. }
  29. [QTabBar](qtabbar.html)::tab:!selected {
  30. margin-top: 2px; /* make non-selected tabs look smaller */
  31. }

通常情况下,我们需要的标签重叠的样子如下:

Qt 样式表 例子 - 图9

对于一个标签控件,看起来像上述情况,我们利用 negative margins. 由此产生的样式表如下:

  1. [QTabWidget](qtabwidget.html)::pane { /* The tab widget frame */
  2. border-top: 2px solid #C2C7CB;
  3. }
  4. [QTabWidget](qtabwidget.html)::tab-bar {
  5. left: 5px; /* move to the right by 5px */
  6. }
  7. /* Style the tab using the tab sub-control. Note that
  8. it reads QTabBar _not_ QTabWidget */
  9. [QTabBar](qtabbar.html)::tab {
  10. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  11. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  12. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  13. border: 2px solid #C4C4C3;
  14. border-bottom-color: #C2C7CB; /* same as the pane color */
  15. border-top-left-radius: 4px;
  16. border-top-right-radius: 4px;
  17. min-width: 8ex;
  18. padding: 2px;
  19. }
  20. [QTabBar](qtabbar.html)::tab:selected, [QTabBar](qtabbar.html)::tab:hover {
  21. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  22. stop: 0 #fafafa, stop: 0.4 #f4f4f4,
  23. stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
  24. }
  25. [QTabBar](qtabbar.html)::tab:selected {
  26. border-color: #9B9B9B;
  27. border-bottom-color: #C2C7CB; /* same as pane color */
  28. }
  29. [QTabBar](qtabbar.html)::tab:!selected {
  30. margin-top: 2px; /* make non-selected tabs look smaller */
  31. }
  32. /* make use of negative margins for overlapping tabs */
  33. [QTabBar](qtabbar.html)::tab:selected {
  34. /* expand/overlap to the left and right by 4px */
  35. margin-left: -4px;
  36. margin-right: -4px;
  37. }
  38. [QTabBar](qtabbar.html)::tab:first:selected {
  39. margin-left: 0; /* the first selected tab has nothing to overlap with on the left */
  40. }
  41. [QTabBar](qtabbar.html)::tab:last:selected {
  42. margin-right: 0; /* the last selected tab has nothing to overlap with on the right */
  43. }
  44. [QTabBar](qtabbar.html)::tab:only-one {
  45. margin: 0; /* if there is only one tab, we don't want overlapping margins */
  46. }

若要将标签栏中心(如下),我们需要以下样式表:

Qt 样式表 例子 - 图10

  1. [QTabWidget](qtabwidget.html)::pane { /* The tab widget frame */
  2. border-top: 2px solid #C2C7CB;
  3. position: absolute;
  4. top: -0.5em;
  5. }
  6. [QTabWidget](qtabwidget.html)::tab-bar {
  7. alignment: center;
  8. }
  9. /* Style the tab using the tab sub-control. Note that
  10. it reads QTabBar _not_ QTabWidget */
  11. [QTabBar](qtabbar.html)::tab {
  12. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  13. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  14. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  15. border: 2px solid #C4C4C3;
  16. border-bottom-color: #C2C7CB; /* same as the pane color */
  17. border-top-left-radius: 4px;
  18. border-top-right-radius: 4px;
  19. min-width: 8ex;
  20. padding: 2px;
  21. }
  22. [QTabBar](qtabbar.html)::tab:selected, [QTabBar](qtabbar.html)::tab:hover {
  23. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  24. stop: 0 #fafafa, stop: 0.4 #f4f4f4,
  25. stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
  26. }
  27. [QTabBar](qtabbar.html)::tab:selected {
  28. border-color: #9B9B9B;
  29. border-bottom-color: #C2C7CB; /* same as pane color */
  30. }

撕裂指示灯和滚动按钮可以进一步自定义如下:

  1. [QTabBar](qtabbar.html)::tear {
  2. image: url(tear_indicator.png);
  3. }
  4. [QTabBar](qtabbar.html)::scroller { /* the width of the scroll buttons */
  5. width: 20px;
  6. }
  7. [QTabBar](qtabbar.html) [QToolButton](qtoolbutton.html) { /* the scroll buttons are tool buttons */
  8. border-image: url(scrollbutton.png) 2;
  9. border-width: 2px;
  10. }
  11. [QTabBar](qtabbar.html) [QToolButton](qtoolbutton.html)::right-arrow { /* the arrow mark in the tool buttons */
  12. image: url(rightarrow.png);
  13. }
  14. [QTabBar](qtabbar.html) [QToolButton](qtoolbutton.html)::left-arrow {
  15. image: url(leftarrow.png);
  16. }

Qt的4.6的关闭按钮可以自定义如下:

  1. [QTabBar](qtabbar.html)::close-button {
  2. image: url(close.png)
  3. subcontrol-position: left;
  4. }
  5. [QTabBar](qtabbar.html)::close-button:hover {
  6. image: url(close-hover.png)
  7. }

自定义 QTableView

假设我们想在我们选择的项目 QTableView 中有泡泡糖粉红色褪色成白色为背景

Qt 样式表 例子 - 图11

这是可能的 selection-background-color 属性和所需的语法是:

  1. [QTableView]($docs-qtableview.html) {
  2. selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0.5, y2: 0.5,
  3. stop: 0 #FF92BB, stop: 1 white);
  4. }

转角小部件可以使用下面的样式表来定制

  1. [QTableView]($docs-qtableview.html) QTableCornerButton::section {
  2. background: red;
  3. border: 2px outset red;
  4. }

自定义 QToolBar

背景和一个句柄 QToolBar 定制如下:

  1. [QToolBar](qtoolbar.html) {
  2. background: red;
  3. spacing: 3px; /* spacing between items in the tool bar */
  4. }
  5. [QToolBar](qtoolbar.html)::handle {
  6. image: url(handle.png);
  7. }

自定义 QToolBox

标签 QToolBox 正在使用的’标签’子控件定制

  1. [QToolBox]($docs-qtoolbox.html)::tab {
  2. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  4. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  5. border-radius: 5px;
  6. color: darkgray;
  7. }
  8. [QToolBox]($docs-qtoolbox.html)::tab:selected { /* italicize selected tabs */
  9. font: italic;
  10. color: white;
  11. }

自定义 QToolButton

有三种类型的QToolButtons

  1. [QToolButton](qtoolbutton.html) { /* all types of tool button */
  2. border: 2px solid #8f8f91;
  3. border-radius: 6px;
  4. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  5. stop: 0 #f6f7fa, stop: 1 #dadbde);
  6. }
  7. [QToolButton](qtoolbutton.html)[popupMode="1"] { /* only for MenuButtonPopup */
  8. padding-right: 20px; /* make way for the popup button */
  9. }
  10. [QToolButton](qtoolbutton.html):pressed {
  11. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  12. stop: 0 #dadbde, stop: 1 #f6f7fa);
  13. }
  14. /* the subcontrols below are used only in the MenuButtonPopup mode */
  15. [QToolButton](qtoolbutton.html)::menu-button {
  16. border: 2px solid gray;
  17. border-top-right-radius: 6px;
  18. border-bottom-right-radius: 6px;
  19. /* 16px width + 4px for border = 20px allocated above */
  20. width: 16px;
  21. }
  22. [QToolButton](qtoolbutton.html)::menu-arrow {
  23. image: url(downarrow.png);
  24. }
  25. [QToolButton](qtoolbutton.html)::menu-arrow:open {
  26. top: 1px; left: 1px; /* shift it a bit */
  27. }

自定义 QToolTip

QToolTip 定制酷似的 QLabel. 此外,用于支持它的平台上,不透明度属性可以被设置为调整不透明度

例如,

  1. [QToolTip](qtooltip.html) {
  2. border: 2px solid darkkhaki;
  3. padding: 5px;
  4. border-radius: 3px;
  5. opacity: 200;
  6. }

自定义 QTreeView

交替行的背景颜色可以用下面的样式表进行定制:

  1. [QTreeView]($docs-qtreeview.html) {
  2. alternate-background-color: yellow;
  3. }

为了提供当你将鼠标悬停在项的特殊背景下,我们可以使用 ::item 子控件。例如,

  1. [QTreeView]($docs-qtreeview.html) {
  2. show-decoration-selected: 1;
  3. }
  4. [QTreeView]($docs-qtreeview.html)::item {
  5. border: 1px solid #d9d9d9;
  6. border-top-color: transparent;
  7. border-bottom-color: transparent;
  8. }
  9. [QTreeView]($docs-qtreeview.html)::item:hover {
  10. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
  11. border: 1px solid #bfcde4;
  12. }
  13. [QTreeView]($docs-qtreeview.html)::item:selected {
  14. border: 1px solid #567dbc;
  15. }
  16. [QTreeView]($docs-qtreeview.html)::item:selected:active{
  17. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
  18. }
  19. [QTreeView]($docs-qtreeview.html)::item:selected:!active {
  20. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
  21. }

一的分支 QTreeView 则使用风格 ::branch 子控件。绘图分支时,以下样式表颜色代码的各种状态。

  1. [QTreeView]($docs-qtreeview.html)::branch {
  2. background: palette(base);
  3. }
  4. [QTreeView]($docs-qtreeview.html)::branch:has-siblings:!adjoins-item {
  5. background: cyan;
  6. }
  7. [QTreeView]($docs-qtreeview.html)::branch:has-siblings:adjoins-item {
  8. background: red;
  9. }
  10. [QTreeView]($docs-qtreeview.html)::branch:!has-children:!has-siblings:adjoins-item {
  11. background: blue;
  12. }
  13. [QTreeView]($docs-qtreeview.html)::branch:closed:has-children:has-siblings {
  14. background: pink;
  15. }
  16. [QTreeView]($docs-qtreeview.html)::branch:has-children:!has-siblings:closed {
  17. background: gray;
  18. }
  19. [QTreeView]($docs-qtreeview.html)::branch:open:has-children:has-siblings {
  20. background: magenta;
  21. }
  22. [QTreeView]($docs-qtreeview.html)::branch:open:has-children:!has-siblings {
  23. background: green;
  24. }

丰富多彩的,虽然它是一个更有用的例子,可以用下面的图像进行:

| Qt 样式表 例子 - 图12 | Qt 样式表 例子 - 图13 | Qt 样式表 例子 - 图14 | Qt 样式表 例子 - 图15 | Qt 样式表 例子 - 图16 | | vline.png | branch-more.png | branch-end.png | branch-closed.png | branch-open.png |

  1. [QTreeView]($docs-qtreeview.html)::branch:has-siblings:!adjoins-item {
  2. border-image: url(vline.png) 0;
  3. }
  4. [QTreeView]($docs-qtreeview.html)::branch:has-siblings:adjoins-item {
  5. border-image: url(branch-more.png) 0;
  6. }
  7. [QTreeView]($docs-qtreeview.html)::branch:!has-children:!has-siblings:adjoins-item {
  8. border-image: url(branch-end.png) 0;
  9. }
  10. [QTreeView]($docs-qtreeview.html)::branch:has-children:!has-siblings:closed,
  11. [QTreeView]($docs-qtreeview.html)::branch:closed:has-children:has-siblings {
  12. border-image: none;
  13. image: url(branch-closed.png);
  14. }
  15. [QTreeView]($docs-qtreeview.html)::branch:open:has-children:!has-siblings,
  16. [QTreeView]($docs-qtreeview.html)::branch:open:has-children:has-siblings {
  17. border-image: none;
  18. image: url(branch-open.png);
  19. }

结果树视图看起来是这样的:

Qt 样式表 例子 - 图17

常见错误 使用样式表时,本节列出一些常见的错误

QPushButton and images

当造型一个 QPushButton, 通常希望使用图像作为按钮图形。这是常见的尝试 background-image 属性,但这有许多缺点:例如,背景会经常出现隐藏按钮装饰后面,因为它不被认为是背景。此外,如果按钮的大小时,整个背景会被拉伸或平铺,这并不总是不好看。

这是更好地利用 border-image 财产,因为它总是会显示图像,无论背景(你可以,如果它有它的alpha值与背景结合起来),它具有特殊的设置来处理按钮大小调整。

考虑下面的代码片段:

  1. [QPushButton](qpushbutton.html) {
  2. color: grey;
  3. border-image: url(/home/kamlie/code/button.png) 3 10 3 10;
  4. border-top: 3px transparent;
  5. border-bottom: 3px transparent;
  6. border-right: 10px transparent;
  7. border-left: 10px transparent;
  8. }

这将产生一个按钮,看起来像这样:

Qt 样式表 例子 - 图18

在URL后的数字分别赋予像素的上,右,下和左数。这些数字对应的边界,不应该当伸展大小的变化。无论何时调整按钮,图像的中间部分将在两个方向上拉伸,而在样式表中不会指定的像素。这使得该按钮的边框看起来更自然,就像这样:

| Qt 样式表 例子 - 图19 | | 圆形边界 |

| Qt 样式表 例子 - 图20 | | 无边界 |

另请参见 Style Sheet Example, Supported HTML Subset, and QStyle.

Qt Style Sheets Reference Qt的样式表参考

| | |