Qt Style Sheets support various properties, pseudo-states, and subcontrols that make it possible to customize the look of widgets.

List of Stylable Widgets

The following table lists the Qt widgets that can be customized using style sheets:

Widget How to Style
QAbstractScrollArea Supports the box model.

All derivatives of QAbstractScrollArea, including QTextEdit, andQAbstractItemView (all item view classes), support scrollable backgrounds using background-attachment. Setting the background-attachment to fixedprovides a background-image that does not scroll with the viewport. Setting the background-attachment to scroll, scrolls the background-image when the scroll bars move.

See Customizing QAbstractScrollArea for an example.

QCheckBox Supports the box model. The check indicator can be styled using the ::indicatorsubcontrol. By default, the indicator is placed in the Top Left corner of the Contents rectangle of the widget.

The spacing property specifies the spacing between the check indicator and the text.

See Customizing QCheckBox for an example.

QColumnView The grip can be styled be using the image property. The arrow indicators can by styled using the ::left-arrow subcontrol and the ::right-arrow subcontrol.
QComboBox The frame around the combobox can be styled using the box model. The drop-down button can be styled using the ::drop-down subcontrol. By default, the drop-down button is placed in the top right corner of the padding rectangle of the widget. The arrow mark inside the drop-down button can be styled using the ::down-arrowsubcontrol. By default, the arrow is placed in the center of the contents rectangle of the drop-down subcontrol.

See Customizing QComboBox for an example.

QDateEdit See QSpinBox.
QDateTimeEdit See QSpinBox.
QDialog Supports only the backgroundbackground-clip and background-origin properties.

Warning: Make sure you define the Q_OBJECT macro for your custom widget.

QDialogButtonBox The layout of buttons can be altered using the button-layout property.
QDockWidget Supports styling of the title bar and the title bar buttons when docked.

The dock widget border can be styled using the border property. The::title subcontrol can be used to customize the title bar. The close and float buttons are positioned with respect to the ::title subcontrol using the ::close-button and ::float-button respectively.

When the title bar is vertical, the :vertical pseudo class is set. In addition, depending on QDockWidget::DockWidgetFeature, the :closable,:floatable and :movable pseudo states are set.

Note: Use QMainWindow::separator to style the resize handle.

Warning: The style sheet has no effect when the QDockWidget is undocked as Qt uses native top level windows when undocked.

See Customizing QDockWidget for an example.

QDoubleSpinBox See QSpinBox.
QFrame Supports the box model.

Since 4.3, setting a stylesheet on a QLabel automatically sets the QFrame::frameStyle property to QFrame::StyledPanel.

See Customizing QFrame for an example.

QGroupBox Supports the box model. The title can be styled using the ::title subcontrol. By default, the title is placed depending on QGroupBox::textAlignment.

In the case of a checkable QGroupBox, the title includes the check indicator. The indicator is styled using the the ::indicator subcontrol. The spacing property can be used to control the spacing between the text and indicator.

See Customizing QGroupBox for an example.

QHeaderView Supports the box model. The sections of the header view are styled using the::section sub control. The section Sub-control supports the :middle:first,:last:only-one:next-selected:previous-selected:selected, and:checked pseudo states.

Sort indicator in can be styled using the ::up-arrow and the ::down-arrowSub-control.

See Customizing QHeaderView for an example.

QLabel Supports the box model. Does not support the :hover pseudo-state.

Since 4.3, setting a stylesheet on a QLabel automatically sets the QFrame::frameStyle property to QFrame::StyledPanel.

See Customizing QFrame for an example (a QLabel derives from QFrame).

QLineEdit Support the box model.

The color and background of the selected item is styled using selection-color and selection-background-color respectively.

The password character can be styled using the lineedit-password-characterproperty.

See Customizing QLineEdit for an example.

QListView Supports the box model. When alternating row colors is enabled, the alternating colors can be styled using the alternate-background-color property.

The color and background of the selected item is styled using selection-color and selection-background-color respectively.

The selection behavior is controlled by the show-decoration-selectedproperty.

Use the ::item subcontrol for more fine grained control over the items in the QListView.

See QAbsractScrollArea to style scrollable backgrounds.

See Customzing QListView for an example.

QListWidget See QListView.
QMainWindow Supports styling of the separator

The separator in a QMainWindow when using QDockWidget is styled using the::separator subcontrol.

See Customizing QMainWindow for an example.

QMenu Supports the box model.

Individual items are styled using the ::item subcontrol. In addition to the usually supported pseudo states, item subcontrol supports the:selected:default:exclusive and the non-exclusive pseudo states.

The indicator of checkable menu items is styled using the ::indicatorsubcontrol.

The separator is styled using the ::separator subcontrol.

For items with a sub menu, the arrow marks are styled using the right-arrow and left-arrow.

The scroller is styled using the ::scroller.

The tear-off is styled using the ::tearoff.

See Customizing QMenu for an example.

QMenuBar Supports the box model. The spacing property specifies the spacing between menu items. Individual items are styled using the ::item subcontrol.

Warning: When running on Qt/Mac, the menu bar is usually embedded into the system-wide menu bar. In this case, the style sheet will have no effect.

See Customizing QMenuBar for an example.

QMessageBox The messagebox-text-interaction-flags property can be used to alter the interaction with text in the message box.
QProgressBar Supports the box model. The chunks of the progress bar can be styled using the::chunk subcontrol. The chunk is displayed on the Contents rectangle of the widget.

If the progress bar displays text, use the text-align property to position the text.

Indeterminate progress bars have the :indeterminate pseudo state set.

See Customizing QProgressBar for an example.

QPushButton Supports the box model. Supports the :default:flat:checked pseudo states.

For QPushButton with a menu, the menu indicator is styled using the::menu-indicator subcontrol. Appearance of checkable push buttons can be customized using the :open and :closed pseudo-states.

Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color. For example,

 QPushButton {  border: none; }

See Customizing QPushButton for an example.

QRadioButton Supports the box model. The check indicator can be styled using the ::indicatorsubcontrol. By default, the indicator is placed in the Top Left corner of the Contents rectangle of the widget.

The spacing property specifies the spacing between the check indicator and the text.

See Customizing QRadioButton for an example.

QScrollBar Supports the box model. The Contents rectangle of the widget is considered to be the groove over which the slider moves. The extent of the QScrollBar (i.e the width or the height depending on the orientation) is set using the width or height property respectively. To determine the orientation, use the :horizontal and the :verticalpseudo states.

The slider can be styled using the ::handle subcontrol. Setting the min-width or min-height provides size contraints for the slider depending on the orientation.

The ::add-line subcontrol can be used to style the button to add a line. By default, the add-line subcontrol is placed in top right corner of the Border rectangle of the widget. Depending on the orientation the ::right-arrow or ::down-arrow. By default, the arrows are placed in the center of the Contents rectangle of the add-line subcontrol.

The ::sub-line subcontrol can be used to style the button to subtract a line. By default, the sub-line subcontrol is placed in bottom right corner of the Border rectangle of the widget. Depending on the orientation the::left-arrow or ::up-arrow. By default, the arrows are placed in the center of the Contents rectangle of the sub-line subcontrol.

The ::sub-page subcontrol can be used to style the region of the slider that subtracts a page. The ::add-page subcontrol can be used to style the region of the slider that adds a page.

See Customizing QScrollBar for an example.

QSizeGrip Supports the widthheight, and image properties.

See Customizing QSizeGrip for an example.

QSlider Supports the box model. For horizontal slides, the min-width and height properties must be provided. For vertical sliders, the min-height and width properties must be provided.

The groove of the slider is styled using the ::groove. The groove is positioned by default in the Contents rectangle of the widget. The thumb of the slider is styled using ::handle subcontrol. The subcontrol moves in the Contents rectangle of the groove subcontrol.

See Customizing QSlider for an example.

QSpinBox The frame of the spin box can be styled using the box model.

The up button and arrow can be styled using the ::up-button and ::up-arrowsubcontrols. By default, the up-button is placed in the top right corner in the Padding rectangle of the widget. Without an explicit size, it occupies half the height of its reference rectangle. The up-arrow is placed in the center of the Contents rectangle of the up-button.

The down button and arrow can be styled using the ::down-button and::down-arrow subcontrols. By default, the down-button is placed in the bottom right corner in the Padding rectangle of the widget. Without an explicit size, it occupies half the height of its reference rectangle. The bottom-arrow is placed in the center of the Contents rectangle of the bottom-button.

See Customizing QSpinBox for an example.

QSplitter Supports the box model. The handle of the splitter is styled using the ::handlesubcontrol.

See Customizing QSplitter for an example.

QStatusBar Supports only the background property. The frame for individual items can be style using the ::item subcontrol.

See Customizing QStatusBar for an example.

QTabBar Individual tabs may be styled using the ::tab subcontrol. Close buttons using the::close-button The tabs support the :only-one:first:last:middle,:previous--selected:next-selected:selected pseudo states.

The :top:left:right:bottom pseudo states depending on the orientation of the tabs.

Overlapping tabs for the selected state are created by using negative margins or using the absolute position scheme.

The tear indicator of the QTabBar is styled using the ::tear subcontrol.

QTabBar used two QToolButtons for its scrollers that can be styled using the QTabBar QToolButton selector. To specify the width of the scroll button use the ::scroller subcontrol.

The alignment of the tabs within the QTabBar is styled using the alignmentproperty.

Warning:

To change the position of the QTabBar within a QTabWidget, use the tab-barsubcontrol (and set subcontrol-position).

See Customizing QTabBar for an example.

QTabWidget The frame of the tab widget is styled using the ::pane subcontrol. The left and right corners are styled using the ::left-corner and ::right-corner respectively. The position of the tab bar is controlled using the ::tab-bar subcontrol.

By default, the subcontrols have positions of a QTabWidget in theQWindowsStyle. To place the QTabBar in the center, set the subcontrol-position of the tab-bar subcontrol.

The :top:left:right:bottom pseudo states depending on the orientation of the tabs.

See Customizing QTabWidget for an example.

QTableView Supports the box model. When alternating row colors is enabled, the alternating colors can be styled using the alternate-background-color property.

The color and background of the selected item is styled using selection-color and selection-background-color respectively.

The corner widget in a QTableView is implemented as a QAbstractButton and can be styled using the "QTableView QTableCornerButton::section" selector.

Warning: If you only set a background-color on a QTableCornerButton, the background may not appear unless you set the border property to some value. This is because, by default, the QTableCornerButton draws a native border which completely overlaps the background-color.

The color of the grid can be specified using the gridline-color property.

See QAbsractScrollArea to style scrollable backgrounds.

See Customzing QTableView for an example.

QTableWidget See QTableView.
QTextEdit Supports the box model.

The color and background of selected text is styled using selection-colorand selection-background-color respectively.

See QAbsractScrollArea to style scrollable backgrounds.

QTimeEdit See QSpinBox.
QToolBar Supports the box model.

The :top:left:right:bottom pseudo states depending on the area in which the tool bar is grouped.

The :first:last:middle:only-one pseudo states indicator the position of the tool bar within a line group (SeeQStyleOptionToolBar::positionWithinLine).

The separator of a QToolBar is styled using the ::separator subcontrol.

The handle (to move the toolbar) is styled using the ::handle subcontrol.

See Customizing QToolBar for an example.

QToolButton Supports the box model.

If the QToolButton has a menu, is ::menu-indicator subcontrol can be used to style the indicator. By default, the menu-indicator is positioned at the bottom right of the Padding rectangle of the widget.

If the QToolButton is in QToolButton::MenuButtonPopup mode, the ::menu-button subcontrol is used to draw the menu button. ::menu-arrow subcontrol is used to draw the menu arrow inside the menu-button. By default, it is positioned in the center of the Contents rectangle of the menu-button subcontrol.

When the QToolButton displays arrows, the ::up-arrow::down-arrow,::left-arrow and ::right-arrow subcontrols are used.

Warning: If you only set a background-color on a QToolButton, the background will not appear unless you set the border property to some value. This is because, by default, the QToolButton draws a native border which completely overlaps the background-color. For example,

 QToolButton { background-color: red; border: none; }

See Customizing QToolButton for an example.

QToolBox Supports the box model.

The individual tabs can by styled using the ::tab subcontrol. The tabs support the :only-one:first:last:middle:previous-selected:next-selected:selected pseudo states.

QToolTip Supports the box model. The opacity property controls the opacity of the tooltip.

See Customizing QFrame for an example (a QToolTip is a QFrame).

QTreeView Supports the box model. When alternating row colors is enabled, the alternating colors can be styled using the alternate-background-color property.

The color and background of the selected item is styled using selection-color and selection-background-color respectively.

The selection behavior is controlled by the show-decoration-selectedproperty.

The branches of the tree view can be styled using the ::branch subcontrol. The ::branch Sub-control supports the :open:closed:has-sibling and:has-children pseudo states.

Use the ::item subcontrol for more fine grained control over the items in the QTreeView.

See QAbsractScrollArea to style scrollable backgrounds.

See Customizing QTreeView for an example to style the branches.

QTreeWidget See QTreeView.
QWidget Supports only the backgroundbackground-clip and background-origin properties.

If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

 void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

The above code is a no-operation if there is no stylesheet set.

Warning: Make sure you define the Q_OBJECT macro for your custom widget.

Qt Style Sheets Reference的更多相关文章

  1. Qt4.7文档翻译:Qt样式单参考,Qt Style Sheets Reference(超长,超全)

    内容目录 Qt样式单参考 可进行样式设置的部件列表 属性列表 图标列表 属性类型列表 伪状态列表 子控件列表 Qt样式单参考 Qt样式单支持各种属性.伪状态和子控件,这样使得妳能够自行设计部件的外观. ...

  2. Qt Style Sheets帮助文档 Overview

    Qt Style Sheets are a powerful mechanism that allows you to customize the appearance of widgets, in ...

  3. Qt Style Sheets制作UI特效

    使用Qt Style Sheets制作UI特效  博客出处:http://developer.nokia.com/community/wiki/%E4%BD%BF%E7%94%A8Qt_Style_S ...

  4. Qt Style Sheets Examples(官方例子目录,很全)

    Contents Style Sheet Usage Customizing the Foreground and Background Colors Customizing Using Dynami ...

  5. 使用Qt Style Sheets制作UI特效

    引言 作为一套GUI框架,Qt是非常强大的.(注:Qt 不仅是一套优秀的GUI框架,同时也是一套出色的应用程序框架).在UI的制作方面Qt为广大开发者提供了一套强大而易用的工具,她就是——Qt Sty ...

  6. Qt Style Sheets Examples(QT真是有很全的文档)

    http://doc.qt.io/qt-5/stylesheet-examples.html http://doc.qt.io/qt-4.8/stylesheet.html

  7. Qt Style Sheets Examples——定制前景色和背景色

    例子取自:http://qt-project.org/doc/qt-4.8/stylesheet-examples.html 以lineEdit为例 (1)设置某个lineEdit的背景色为黄色 li ...

  8. Qt 外观之一 ——Qt Style Sheet

    Qt Style Sheet 目录 使用 对于应用程序 创建自定义控件 QSS语法 一般选择器(selector) 伪选择器 解决冲突 使用specificity Namespace冲突 级联效应 设 ...

  9. Qt Style Sheet实践(一):按钮及关联菜单

    导读 正如web前端开发中CSS(Cascade Style Sheet)的作用一样,Qt开发中也可以使用修改版的QSS将逻辑业务和用户界面进行隔离.这样,美工设计人员和逻辑实现者可以各司其职而不受干 ...

随机推荐

  1. 创建cocos项目

    创建cocos项目 进入终端输入命令: cocos new HelloCpp -p com.cocos.org -l cpp -d /Users/emerys/documents/cocos 参数说明 ...

  2. C# == 和equals()区别

    如以下代码: ? 1 2 3 4 5 int age = 25; short newAge = 25; Console.WriteLine(age == newAge);  //true Consol ...

  3. 关于auto和decltype

    auto会忽略顶层const,保留底层const ; const int* const p = &i; auto p2 = p; //p2是const int*,不是const int* co ...

  4. Q我音乐

  5. Unique Binary Search Tree

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. 初涉JavaScript模式 (12) : 沙箱模式

    引子 上一篇说了模块模式,而对于其中的命名空间模式其实也是有着一些问题,比如每添加一个模块或则深入叠加都会导致长命名,并且对于多个库的不同版本同时运行,一不小心就会污染全局标识,而这两天也发现了JSe ...

  8. Thinkphp---练习:数据的增删改查

    利用ThinkPHP连接数据库的增删改查的例题:用到的数据库表名Info表,Nation表 数据显示页面:MainController.class.php中的方法(增删改查全包括--function ...

  9. 利用Php ssh2扩展实现svn自动提交到测试服务器

    1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...

  10. C# 部分关键字

    关键字: virtual:  虚方法,本身可以被实例化,也可以在派生类中重写该方法: override:在派生类重写基类虚方法时声明,避免了C++中的潜在运行错误: abstract:声明为抽象类.抽 ...