Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 5890

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- stylesheet.qdoc -->
<head>
  <title>Qt 4.6: Qt Style Sheets Examples</title>
  <link rel="prev" href="stylesheet-reference.html" />
  <link rel="contents" href="stylesheet.html" />
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><p>
[Previous: <a href="stylesheet-reference.html">Qt Style Sheets Reference</a>]
[<a href="stylesheet.html">Contents</a>]
</p>
<h1 class="title">Qt Style Sheets Examples<br /><span class="subtitle"></span>
</h1>
<p>We will now see a few examples to get started with using Qt Style Sheets.</p>
<ul><li><a href="#style-sheet-usage">Style Sheet Usage</a></li>
<ul><li><a href="#customizing-the-foreground-and-background-colors">Customizing the Foreground and Background Colors</a></li>
<li><a href="#customizing-using-dynamic-properties">Customizing Using Dynamic Properties</a></li>
<li><a href="#customizing-a-qpushbutton-using-the-box-model">Customizing a QPushButton Using the Box Model</a></li>
<li><a href="#customizing-the-qpushbutton-s-menu-indicator-sub-control">Customizing the QPushButton's Menu Indicator Sub-Control</a></li>
<li><a href="#complex-selector-example">Complex Selector Example</a></li>
</ul>
<li><a href="#customizing-specific-widgets">Customizing specific widgets</a></li>
<ul><li><a href="#customizing-qabstractscrollarea">Customizing QAbstractScrollArea</a></li>
<li><a href="#customizing-qcheckbox">Customizing QCheckBox</a></li>
<li><a href="#customizing-qcombobox">Customizing QComboBox</a></li>
<li><a href="#customizing-qdockwidget">Customizing QDockWidget</a></li>
<li><a href="#customizing-qframe">Customizing QFrame</a></li>
<li><a href="#customizing-qgroupbox">Customizing QGroupBox</a></li>
<li><a href="#customizing-qheaderview">Customizing QHeaderView</a></li>
<li><a href="#customizing-qlineedit">Customizing QLineEdit</a></li>
<li><a href="#customizing-qlistview">Customizing QListView</a></li>
<li><a href="#customizing-qmainwindow">Customizing QMainWindow</a></li>
<li><a href="#customizing-qmenu">Customizing QMenu</a></li>
<li><a href="#customizing-qmenubar">Customizing QMenuBar</a></li>
<li><a href="#customizing-qprogressbar">Customizing QProgressBar</a></li>
<li><a href="#customizing-qpushbutton">Customizing QPushButton</a></li>
<li><a href="#customizing-qradiobutton">Customizing QRadioButton</a></li>
<li><a href="#customizing-qscrollbar">Customizing QScrollBar</a></li>
<li><a href="#customizing-qsizegrip">Customizing QSizeGrip</a></li>
<li><a href="#customizing-qslider">Customizing QSlider</a></li>
<li><a href="#customizing-qspinbox">Customizing QSpinBox</a></li>
<li><a href="#customizing-qsplitter">Customizing QSplitter</a></li>
<li><a href="#customizing-qstatusbar">Customizing QStatusBar</a></li>
<li><a href="#customizing-qtabwidget-and-qtabbar">Customizing QTabWidget and QTabBar</a></li>
<li><a href="#customizing-qtableview">Customizing QTableView</a></li>
<li><a href="#customizing-qtoolbar">Customizing QToolBar</a></li>
<li><a href="#customizing-qtoolbox">Customizing QToolBox</a></li>
<li><a href="#customizing-qtoolbutton">Customizing QToolButton</a></li>
<li><a href="#customizing-qtooltip">Customizing QToolTip</a></li>
<li><a href="#customizing-qtreeview">Customizing QTreeView</a></li>
</ul>
<li><a href="#common-mistakes">Common mistakes</a></li>
<ul><li><a href="#qpushbutton-and-images">QPushButton and images</a></li>
</ul>
</ul>
<a name="style-sheet-usage"></a>
<h2>Style Sheet Usage</h2>
<a name="customizing-the-foreground-and-background-colors"></a>
<h3>Customizing the Foreground and Background Colors</h3>
<p>Let's start by setting yellow as the background color of all <a href="qlineedit.html">QLineEdit</a>s in an application. This could be achieved like this:</p>
<pre> qApp-&gt;setStyleSheet(&quot;QLineEdit { background-color: yellow }&quot;);</pre>
<p>If we want the property to apply only to the <a href="qlineedit.html">QLineEdit</a>s that are children (or grandchildren or grand-grandchildren) of a specific dialog, we would rather do this:</p>
<pre> myDialog-&gt;setStyleSheet(&quot;QLineEdit { background-color: yellow }&quot;);</pre>
<p>If we want the property to apply only to one specific <a href="qlineedit.html">QLineEdit</a>, we can give it a name using <a href="qobject.html#objectName-prop">QObject::setObjectName</a>() and use an ID Selector to refer to it:</p>
<pre> myDialog-&gt;setStyleSheet(&quot;QLineEdit#nameEdit { background-color: yellow }&quot;);</pre>
<p>Alternatively, we can set the <a href="stylesheet-reference.html#background-prop">background-color</a> property directly on the <a href="qlineedit.html">QLineEdit</a>, omitting the selector:</p>
<pre> nameEdit-&gt;setStyleSheet(&quot;background-color: yellow&quot;);</pre>
<p>To ensure a good contrast, we should also specify a suitable color for the text:</p>
<pre> nameEdit-&gt;setStyleSheet(&quot;color: blue; background-color: yellow&quot;);</pre>
<p>It might be a good idea to change the colors used for selected text as well:</p>
<pre> nameEdit-&gt;setStyleSheet(&quot;color: blue;&quot;
                         &quot;background-color: yellow;&quot;
                         &quot;selection-color: yellow;&quot;
                         &quot;selection-background-color: blue;&quot;);</pre>
<a name="customizing-using-dynamic-properties"></a>
<h3>Customizing Using Dynamic Properties</h3>
<p>There are many situations where we need to present a form that has mandatory fields. To indicate to the user that the field is mandatory, one effective (albeit esthetically dubious) solution is to use yellow as the background color for those fields. It turns out this is very easy to implement using Qt Style Sheets. First, we would use the following application-wide style sheet:</p>
<pre> *[mandatoryField=&quot;true&quot;] { background-color: yellow }</pre>
<p>This means that every widget whose <tt>mandatoryField</tt> Qt property is set to true would have a yellow background.</p>
<p>Then, for each mandatory field widget, we would simply create a <tt>mandatoryField</tt> property on the fly and set it to true. For example:</p>
<pre> QLineEdit *nameEdit = new QLineEdit(this);
 nameEdit-&gt;setProperty(&quot;mandatoryField&quot;, true);

 QLineEdit *emailEdit = new QLineEdit(this);
 emailEdit-&gt;setProperty(&quot;mandatoryField&quot;, true);

 QSpinBox *ageSpinBox = new QSpinBox(this);
 ageSpinBox-&gt;setProperty(&quot;mandatoryField&quot;, true);</pre>
<a name="customizing-a-qpushbutton-using-the-box-model"></a>
<h3>Customizing a QPushButton Using the Box Model</h3>
<p>This time, we will show how to create a red <a href="qpushbutton.html">QPushButton</a>. This <a href="qpushbutton.html">QPushButton</a> would presumably be connected to a very destructive piece of code.</p>
<p>First, we are tempted to use this style sheet:</p>
<pre> QPushButton#evilButton { background-color: red }</pre>
<p>However, the result is a boring, flat button with no borders:</p>
<p align="center"><img src="images/stylesheet-redbutton1.png" alt="A flat red button" /></p><p>What happened is this:</p>
<ul>
<li>We have made a request that cannot be satisfied using the native styles alone (e.g&#x2e;, the Windows XP theme engine doesn't let us specify the background color of a button).</li>
<li>Therefore, the button is rendered using style sheets.</li>
<li>We haven't specified any values for <a href="stylesheet-reference.html#border-width-prop">border-width</a> and <a href="stylesheet-reference.html#border-style-prop">border-style</a>, so by default we obtain a 0-pixel wide border of style <tt>none</tt>.</li>
</ul>
<p>Let's improve the situation by specifying a border:</p>
<pre> QPushButton#evilButton {
     background-color: red;
     border-style: outset;
     border-width: 2px;
     border-color: beige;
 }</pre>
<p align="center"><img src="images/stylesheet-redbutton2.png" alt="A red button with a beige border" /></p><p>Things look already a lot better. But the button looks a bit cramped. Let's specify some spacing between the border and the text using the <a href="stylesheet-reference.html#padding-prop">padding</a>. Additionally, we will enforce a minimum width, round the corners, and specify a larger font to make the button look nicer:</p>
<pre> QPushButton#evilButton {
     background-color: red;
     border-style: outset;
     border-width: 2px;
     border-radius: 10px;
     border-color: beige;
     font: bold 14px;
     min-width: 10em;
     padding: 6px;
 }</pre>
<p align="center"><img src="images/stylesheet-redbutton3.png" alt="A red button with a round beige border and big, bold text" /></p><p>The only issue remaining is that the button doesn't react when we press it. We can fix this by specifying a slightly different background color and use a different border style.</p>
<pre> QPushButton#evilButton {
     background-color: red;
     border-style: outset;
     border-width: 2px;
     border-radius: 10px;
     border-color: beige;
     font: bold 14px;
     min-width: 10em;
     padding: 6px;
 }
 QPushButton#evilButton:pressed {
     background-color: rgb(224, 0, 0);
     border-style: inset;
 }</pre>
<a name="customizing-the-qpushbutton-s-menu-indicator-sub-control"></a>
<h3>Customizing the QPushButton's Menu Indicator Sub-Control</h3>
<p>Subcontrols give access to the sub-elements of a widget. For example, a <a href="qpushbutton.html">QPushButton</a> associated with a menu (using <a href="qpushbutton.html#setMenu">QPushButton::setMenu</a>()) has a menu indicator. Let's customize the menu indicator for the red push button:</p>
<pre> QPushButton#evilButton::menu-indicator {
     image: url(myindicator.png);
 }</pre>
<p>By default, the menu indicator is located at the bottom-right corner of the padding rectangle. We can change this by specifying <a href="stylesheet-reference.html#subcontrol-position-prop">subcontrol-position</a> and <a href="stylesheet-reference.html#subcontrol-origin-prop">subcontrol-origin</a> to anchor the indicator differently. We can also use <a href="stylesheet-reference.html#top-prop">top</a> and <a href="stylesheet-reference.html#left-prop">left</a> to move the indicator by a few pixels. For example:</p>
<pre> QPushButton::menu-indicator {
     image: url(myindicator.png);
     subcontrol-position: right center;
     subcontrol-origin: padding;
     left: -2px;
 }</pre>
<p>This positions the <tt>myindicator.png</tt> to the center right of the <a href="qpushbutton.html">QPushButton</a>'s <a href="stylesheet-reference.html#padding-prop">padding</a> rectangle (see <a href="stylesheet-reference.html#subcontrol-origin-prop">subcontrol-origin</a> for more information).</p>
<a name="complex-selector-example"></a>
<h3>Complex Selector Example</h3>
<p>Since red seems to be our favorite color, let's make the text in <a href="qlineedit.html">QLineEdit</a> red by setting the following application-wide stylesheet:</p>
<pre> QLineEdit { color: red }</pre>
<p>However, we would like to give a visual indication that a <a href="qlineedit.html">QLineEdit</a> is read-only by making it appear gray:</p>
<pre> QLineEdit { color: red }
 QLineEdit[readOnly=&quot;true&quot;] { color: gray }</pre>
<p>At some point, our design team comes with the requirement that all <a href="qlineedit.html">QLineEdit</a>s in the registration form (with the <a href="qobject.html#objectName-prop">object name</a> <tt>registrationDialog</tt>) to be brown:</p>
<pre> QLineEdit { color: red }
 QLineEdit[readOnly=&quot;true&quot;] { color: gray }
 #registrationDialog QLineEdit { color: brown }</pre>
<p>A few UI design meetings later, we decide that all our <a href="qdialog.html">QDialog</a>s should have brown colored <a href="qlineedit.html">QLineEdit</a>s:</p>
<pre> QLineEdit { color: red }
 QLineEdit[readOnly=&quot;true&quot;] { color: gray }
 QDialog QLineEdit { color: brown }</pre>
<p>Quiz: What happens if we have a read-only <a href="qlineedit.html">QLineEdit</a> in a <a href="qdialog.html">QDialog</a>? [Hint: The <a href="stylesheet-syntax.html#conflict-resolution">Conflict Resolution</a> section above explains what happens in cases like this.]</p>
<a name="customizing-specific-widgets"></a>
<h2>Customizing specific widgets</h2>
<p>This section provides examples to customize specific widgets using Style Sheets.</p>
<a name="customizing-qabstractscrollarea"></a>
<h3>Customizing QAbstractScrollArea</h3>
<p>The background of any <a href="qabstractscrollarea.html">QAbstractScrollArea</a> (Item views, <a href="qtextedit.html">QTextEdit</a> and <a href="qtextbrowser.html">QTextBrowser</a>) can be set using the background properties. For example, to set a background-image that scrolls with the scroll bar:</p>
<pre> QTextEdit, QListView {
     background-color: white;
     background-image: url(draft.png);
     background-attachment: scroll;
 }</pre>
<p>If the background-image is to be fixed with the viewport:</p>
<pre> QTextEdit, QListView {
     background-color: white;
     background-image: url(draft.png);
     background-attachment: fixed;
 }</pre>
<a name="customizing-qcheckbox"></a>
<h3>Customizing QCheckBox</h3>
<p>Styling of a <a href="qcheckbox.html">QCheckBox</a> is almost indentical to styling a <a href="qradiobutton.html">QRadioButton</a>. The main difference is that a tristate <a href="qcheckbox.html">QCheckBox</a> has an indeterminate state.</p>
<pre> QCheckBox {
     spacing: 5px;
 }

 QCheckBox::indicator {
     width: 13px;
     height: 13px;
 }

 QCheckBox::indicator:unchecked {
     image: url(:/images/checkbox_unchecked.png);
 }

 QCheckBox::indicator:unchecked:hover {
     image: url(:/images/checkbox_unchecked_hover.png);
 }

 QCheckBox::indicator:unchecked:pressed {
     image: url(:/images/checkbox_unchecked_pressed.png);
 }

 QCheckBox::indicator:checked {
     image: url(:/images/checkbox_checked.png);
 }

 QCheckBox::indicator:checked:hover {
     image: url(:/images/checkbox_checked_hover.png);
 }

 QCheckBox::indicator:checked:pressed {
     image: url(:/images/checkbox_checked_pressed.png);
 }

 QCheckBox::indicator:indeterminate:hover {
     image: url(:/images/checkbox_indeterminate_hover.png);
 }

 QCheckBox::indicator:indeterminate:pressed {
     image: url(:/images/checkbox_indeterminate_pressed.png);
 }</pre>
<a name="customizing-qcombobox"></a>
<h3>Customizing QComboBox</h3>
<p>We will look at an example where the drop down button of a <a href="qcombobox.html">QComboBox</a> appears &quot;merged&quot; with the combo box frame.</p>
<pre> QComboBox {
     border: 1px solid gray;
     border-radius: 3px;
     padding: 1px 18px 1px 3px;
     min-width: 6em;
 }

 QComboBox:editable {
     background: white;
 }

 QComboBox:!editable, QComboBox::drop-down:editable {
      background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                  stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                  stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
 }

 /* QComboBox gets the &quot;on&quot; state when the popup is open */
 QComboBox:!editable:on, QComboBox::drop-down:editable:on {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,
                                 stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);
 }

 QComboBox:on { /* shift the text when the popup opens */
     padding-top: 3px;
     padding-left: 4px;
 }

 QComboBox::drop-down {
     subcontrol-origin: padding;
     subcontrol-position: top right;
     width: 15px;

     border-left-width: 1px;
     border-left-color: darkgray;
     border-left-style: solid; /* just a single line */
     border-top-right-radius: 3px; /* same radius as the QComboBox */
     border-bottom-right-radius: 3px;
 }

 QComboBox::down-arrow {
     image: url(/usr/share/icons/crystalsvg/16x16/actions/1downarrow.png);
 }

 QComboBox::down-arrow:on { /* shift the arrow when popup is open */
     top: 1px;
     left: 1px;
 }</pre>
<p>The pop-up of the <a href="qcombobox.html">QComboBox</a> is a <a href="qabstractitemview.html">QAbstractItemView</a> and is styled using the descendant selector:</p>
<pre> QComboBox QAbstractItemView {
     border: 2px solid darkgray;
     selection-background-color: lightgray;
 }</pre>
<a name="customizing-qdockwidget"></a>
<h3>Customizing QDockWidget</h3>
<p>The title bar and the buttons of a <a href="qdockwidget.html">QDockWidget</a> can be customized as follows:</p>
<pre> QDockWidget {
     border: 1px solid lightgray;
     titlebar-close-icon: url(close.png);
     titlebar-normal-icon: url(undock.png);
 }

 QDockWidget::title {
     text-align: left; /* align the text to the left */
     background: lightgray;
     padding-left: 5px;
 }

 QDockWidget::close-button, QDockWidget::float-button {
     border: 1px solid transparent;
     background: darkgray;
     padding: 0px;
 }

 QDockWidget::close-button:hover, QDockWidget::float-button:hover {
     background: gray;
 }

 QDockWidget::close-button:pressed, QDockWidget::float-button:pressed {
     padding: 1px -1px -1px 1px;
 }</pre>
<p>If one desires to move the dock widget buttons to the left, the following style sheet can be used:</p>
<pre> QDockWidget {
     border: 1px solid lightgray;
     titlebar-close-icon: url(close.png);
     titlebar-normal-icon: url(float.png);
 }

 QDockWidget::title {
     text-align: left;
     background: lightgray;
     padding-left: 35px;
 }

 QDockWidget::close-button, QDockWidget::float-button {
     background: darkgray;
     padding: 0px;
     icon-size: 14px; /* maximum icon size */
 }

 QDockWidget::close-button:hover, QDockWidget::float-button:hover {
     background: gray;
 }

 QDockWidget::close-button:pressed, QDockWidget::float-button:pressed {
     padding: 1px -1px -1px 1px;
 }

 QDockWidget::close-button {
     subcontrol-position: top left;
     subcontrol-origin: margin;
     position: absolute;
     top: 0px; left: 0px; bottom: 0px;
     width: 14px;
 }

 QDockWidget::float-button {
     subcontrol-position: top left;
     subcontrol-origin: margin;
     position: absolute;
     top: 0px; left: 16px; bottom: 0px;
     width: 14px;
 }</pre>
<p><b>Note:</b> To customize the separator (resize handle) of a <a href="qdockwidget.html">QDockWidget</a>, use QMainWindow::separator.</p>
<a name="customizing-qframe"></a>
<h3>Customizing QFrame</h3>
<p>A <a href="qframe.html">QFrame</a> is styled using the <a href="stylesheet-customizing.html#the-box-model">The Box Model</a>.</p>
<pre> QFrame, QLabel, QToolTip {
     border: 2px solid green;
     border-radius: 4px;
     padding: 2px;
     background-image: url(images/welcome.png);
 }</pre>
<a name="customizing-qgroupbox"></a>
<h3>Customizing QGroupBox</h3>
<p>Let us look at an example that moves the <a href="qgroupbox.html">QGroupBox</a>'s title to the center.</p>
<pre> QGroupBox {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #E0E0E0, stop: 1 #FFFFFF);
     border: 2px solid gray;
     border-radius: 5px;
     margin-top: 1ex; /* leave space at the top for the title */
 }

 QGroupBox::title {
     subcontrol-origin: margin;
     subcontrol-position: top center; /* position at the top center */
     padding: 0 3px;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #FFOECE, stop: 1 #FFFFFF);
 }</pre>
<p>For a checkable <a href="qgroupbox.html">QGroupBox</a>, use the {#indicator-sub}{::indicator} subcontrol and style it exactly like a <a href="qcheckbox.html">QCheckBox</a> (i.e)</p>
<pre> QGroupBox::indicator {
     width: 13px;
     height: 13px;
 }

 QGroupBox::indicator:unchecked {
     image: url(:/images/checkbox_unchecked.png);
 }

 /* proceed with styling just like QCheckBox */</pre>
<a name="customizing-qheaderview"></a>
<h3>Customizing QHeaderView</h3>
<p><a href="qheaderview.html">QHeaderView</a> is customized as follows:</p>
<pre> QHeaderView::section {
     background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                       stop:0 #616161, stop: 0.5 #505050,
                                       stop: 0.6 #434343, stop:1 #656565);
     color: white;
     padding-left: 4px;
     border: 1px solid #6c6c6c;
 }

 QHeaderView::section:checked
 {
     background-color: red;
 }

 /* style the sort indicator */
 QHeaderView::down-arrow {
     image: url(down_arrow.png);
 }

 QHeaderView::up-arrow {
     image: url(up_arrow.png);
 }</pre>
<a name="customizing-qlineedit"></a>
<h3>Customizing QLineEdit</h3>
<p>The frame of a <a href="qlineedit.html">QLineEdit</a> is styled using the <a href="stylesheet-customizing.html#the-box-model">The Box Model</a>. To create a line edit with rounded corners, we can set:</p>
<pre> QLineEdit {
     border: 2px solid gray;
     border-radius: 10px;
     padding: 0 8px;
     background: yellow;
     selection-background-color: darkgray;
 }</pre>
<p>The password character of line edits that have <a href="qlineedit.html#EchoMode-enum">QLineEdit::Password</a> echo mode can be set using:</p>
<pre> QLineEdit[echoMode=&quot;2&quot;] {
     lineedit-password-character: 9679;
 }</pre>
<p>The background of a read only <a href="qlineedit.html">QLineEdit</a> can be modified as below:</p>
<pre> QLineEdit:read-only {
     background: lightblue;</pre>
<a name="customizing-qlistview"></a>
<h3>Customizing QListView</h3>
<p>The background color of alternating rows can be customized using the following style sheet:</p>
<pre> QListView {
     alternate-background-color: yellow;
 }</pre>
<p>To provide a special background when you hover over items, we can use the <a href="stylesheet-reference.html#item-sub">::item</a> subcontrol. For example,</p>
<pre> QListView {
     show-decoration-selected: 1; /* make the selection span the entire width of the view */
 }

 QListView::item:alternate {
     background: #EEEEEE;
 }

 QListView::item:selected {
     border: 1px solid #6a6ea9;
 }

 QListView::item:selected:!active {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #ABAFE5, stop: 1 #8588B2);
 }

 QListView::item:selected:active {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #6a6ea9, stop: 1 #888dd9);
 }

 QListView::item:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #FAFBFE, stop: 1 #DCDEF1);
 }</pre>
<a name="customizing-qmainwindow"></a>
<h3>Customizing QMainWindow</h3>
<p>The separator of a <a href="qmainwindow.html">QMainWindow</a> can be styled as follows:</p>
<pre> QMainWindow::separator {
     background: yellow;
     width: 10px; /* when vertical */
     height: 10px; /* when horizontal */
 }

 QMainWindow::separator:hover {
     background: red;
 }</pre>
<a name="customizing-qmenu"></a>
<h3>Customizing QMenu</h3>
<p>Individual items of a <a href="qmenu.html">QMenu</a> are styled using the 'item' subcontrol as follows:</p>
<pre> QMenu {
     background-color: #ABABAB; /* sets background of the menu */
     border: 1px solid black;
 }

 QMenu::item {
     /* sets background of menu item. set this to something non-transparent
         if you want menu color and menu item color to be different */
     background-color: transparent;
 }

 QMenu::item:selected { /* when user selects item using mouse or keyboard */
     background-color: #654321;
 }</pre>
<p>For a more advanced customization, use a style sheet as follows:</p>
<pre> QMenu {
     background-color: white;
     margin: 2px; /* some spacing around the menu */
 }

 QMenu::item {
     padding: 2px 25px 2px 20px;
     border: 1px solid transparent; /* reserve space for selection border */
 }

 QMenu::item:selected {
     border-color: darkblue;
     background: rgba(100, 100, 100, 150);
 }

 QMenu::icon:checked { /* appearance of a 'checked' icon */
     background: gray;
     border: 1px inset gray;
     position: absolute;
     top: 1px;
     right: 1px;
     bottom: 1px;
     left: 1px;
 }

 QMenu::separator {
     height: 2px;
     background: lightblue;
     margin-left: 10px;
     margin-right: 5px;
 }

 QMenu::indicator {
     width: 13px;
     height: 13px;
 }

 /* non-exclusive indicator = check box style indicator (see QActionGroup::setExclusive) */
 QMenu::indicator:non-exclusive:unchecked {
     image: url(:/images/checkbox_unchecked.png);
 }

 QMenu::indicator:non-exclusive:unchecked:selected {
     image: url(:/images/checkbox_unchecked_hover.png);
 }

 QMenu::indicator:non-exclusive:checked {
     image: url(:/images/checkbox_checked.png);
 }

 QMenu::indicator:non-exclusive:checked:selected {
     image: url(:/images/checkbox_checked_hover.png);
 }

 /* exclusive indicator = radio button style indicator (see QActionGroup::setExclusive) */
 QMenu::indicator:exclusive:unchecked {
     image: url(:/images/radiobutton_unchecked.png);
 }

 QMenu::indicator:exclusive:unchecked:selected {
     image: url(:/images/radiobutton_unchecked_hover.png);
 }

 QMenu::indicator:exclusive:checked {
     image: url(:/images/radiobutton_checked.png);
 }

 QMenu::indicator:exclusive:checked:selected {
     image: url(:/images/radiobutton_checked_hover.png);
 }</pre>
<a name="customizing-qmenubar"></a>
<h3>Customizing QMenuBar</h3>
<p><a href="qmenubar.html">QMenuBar</a> is styled as follows:</p>
<pre> QMenuBar {
     background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                       stop:0 lightgray, stop:1 darkgray);
 }

 QMenuBar::item {
     spacing: 3px; /* spacing between menu bar items */
     padding: 1px 4px;
     background: transparent;
     border-radius: 4px;
 }

 QMenuBar::item:selected { /* when selected using mouse or keyboard */
     background: #a8a8a8;
 }

 QMenuBar::item:pressed {
     background: #888888;
 }</pre>
<a name="customizing-qprogressbar"></a>
<h3>Customizing QProgressBar</h3>
<p>The <a href="qprogressbar.html">QProgressBar</a>'s <a href="stylesheet-reference.html#border-prop">border</a>, <a href="stylesheet-reference.html#chunk-sub">chunk</a>, and <a href="stylesheet-reference.html#text-align-prop">text-align</a> can be customized using style sheets. However, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.</p>
<p align="center"><img src="images/progressBar-stylesheet.png" /></p><p>For example, we change the <a href="stylesheet-reference.html#border-prop">border</a> to grey and the <a href="stylesheet-reference.html#chunk-sub">chunk</a> to cerulean.</p>
<pre> QProgressBar {
     border: 2px solid grey;
     border-radius: 5px;
 }

 QProgressBar::chunk {
     background-color: #05B8CC;
     width: 20px;
 }</pre>
<p>This leaves the <a href="stylesheet-reference.html#text-align-prop">text-align</a>, which we customize by positioning the text in the center of the progress bar.</p>
<pre> QProgressBar {
     border: 2px solid grey;
     border-radius: 5px;
     text-align: center;
 }</pre>
<p>A <a href="stylesheet-reference.html#margin-prop">margin</a> can be included to obtain more visible chunks.</p>
<p align="center"><img src="images/progressBar2-stylesheet.png" /></p><p>In the screenshot above, we use a <a href="stylesheet-reference.html#margin-prop">margin</a> of 0.5 pixels.</p>
<pre> QProgressBar::chunk {
     background-color: #CD96CD;
     width: 10px;
     margin: 0.5px;
 }</pre>
<a name="customizing-qpushbutton"></a>
<h3>Customizing QPushButton</h3>
<p>A <a href="qpushbutton.html">QPushButton</a> is styled as follows:</p>
<pre> QPushButton {
     border: 2px solid #8f8f91;
     border-radius: 6px;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #f6f7fa, stop: 1 #dadbde);
     min-width: 80px;
 }

 QPushButton:pressed {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #dadbde, stop: 1 #f6f7fa);
 }

 QPushButton:flat {
     border: none; /* no border for a flat push button */
 }

 QPushButton:default {
     border-color: navy; /* make the default button prominent */
 }</pre>
<p>For a <a href="qpushbutton.html">QPushButton</a> with a menu, use the <a href="stylesheet-reference.html#menu-indicator-sub">::menu-indicator</a> subcontrol.</p>
<pre> QPushButton:open { /* when the button has its menu open */
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #dadbde, stop: 1 #f6f7fa);
 }

 QPushButton::menu-indicator {
     image: url(menu_indicator.png);
     subcontrol-origin: padding;
     subcontrol-position: bottom right;
 }

 QPushButton::menu-indicator:pressed, QPushButton::menu-indicator:open {
     position: relative;
     top: 2px; left: 2px; /* shift the arrow by 2 px */
 }</pre>
<p>Checkable <a href="qpushbutton.html">QPushButton</a> have the <a href="stylesheet-reference.html#checked-ps">:checked</a> pseudo state set.</p>
<a name="customizing-qradiobutton"></a>
<h3>Customizing QRadioButton</h3>
<p>The indicator of a <a href="qradiobutton.html">QRadioButton</a> can be changed using:</p>
<pre> QRadioButton::indicator {
     width: 13px;
     height: 13px;
 }

 QRadioButton::indicator::unchecked {
     image: url(:/images/radiobutton_unchecked.png);
 }

 QRadioButton::indicator:unchecked:hover {
     image: url(:/images/radiobutton_unchecked_hover.png);
 }

 QRadioButton::indicator:unchecked:pressed {
     image: url(:/images/radiobutton_unchecked_pressed.png);
 }

 QRadioButton::indicator::checked {
     image: url(:/images/radiobutton_checked.png);
 }

 QRadioButton::indicator:checked:hover {
     image: url(:/images/radiobutton_checked_hover.png);
 }

 QRadioButton::indicator:checked:pressed {
     image: url(:/images/radiobutton_checked_pressed.png);
 }</pre>
<a name="customizing-qscrollbar"></a>
<h3>Customizing QScrollBar</h3>
<p>The <a href="qscrollbar.html">QScrollBar</a> can be styled using its subcontrols like <a href="stylesheet-reference.html#handle-sub">handle</a>, <a href="stylesheet-reference.html#add-line-sub">add-line</a>, <a href="stylesheet-reference.html#sub-line-sub">sub-line</a>, and so on. Note that if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.</p>
<p align="center"><img src="images/stylesheet-scrollbar1.png" /></p><p>The scroll bar above has been styled in aquamarine with a solid grey border.</p>
<pre> QScrollBar:horizontal {
     border: 2px solid grey;
     background: #32CC99;
     height: 15px;
     margin: 0px 20px 0 20px;
 }
 QScrollBar::handle:horizontal {
     background: white;
     min-width: 20px;
 }
 QScrollBar::add-line:horizontal {
     border: 2px solid grey;
     background: #32CC99;
     width: 20px;
     subcontrol-position: right;
     subcontrol-origin: margin;
 }

 QScrollBar::sub-line:horizontal {
     border: 2px solid grey;
     background: #32CC99;
     width: 20px;
     subcontrol-position: left;
     subcontrol-origin: margin;
 }</pre>
<p>The <a href="stylesheet-reference.html#left-arrow-sub">left-arrow</a> and <a href="stylesheet-reference.html#right-arrow-sub">right-arrow</a> have a solid grey border with a white background. As an alternative, you could also embed the image of an arrow.</p>
<pre> QScrollBar:left-arrow:horizontal, QScrollBar::right-arrow:horizontal {
     border: 2px solid grey;
     width: 3px;
     height: 3px;
     background: white;
 }

 QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {
     background: none;
 }</pre>
<p>If you want the scroll buttons of the scroll bar to be placed together (instead of the edges) like on Mac OS X, you can use the following stylesheet:</p>
<pre> QScrollBar:horizontal {
     border: 2px solid green;
     background: cyan;
     height: 15px;
     margin: 0px 40px 0 0px;
 }

 QScrollBar::handle:horizontal {
     background: gray;
     min-width: 20px;
 }

 QScrollBar::add-line:horizontal {
     background: blue;
     width: 16px;
     subcontrol-position: right;
     subcontrol-origin: margin;
     border: 2px solid black;
 }

 QScrollBar::sub-line:horizontal {
     background: magenta;
     width: 16px;
     subcontrol-position: top right;
     subcontrol-origin: margin;
     border: 2px solid black;
     position: absolute;
     right: 20px;
 }

 QScrollBar:left-arrow:horizontal, QScrollBar::right-arrow:horizontal {
     width: 3px;
     height: 3px;
     background: pink;
 }

 QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {
     background: none;
 }</pre>
<p>The scroll bar using the above stylesheet looks like this:</p>
<p align="center"><img src="images/stylesheet-scrollbar2.png" /></p><p>To customize a vertical scroll bar use a style sheet similar to the following:</p>
<pre>  QScrollBar:vertical {
      border: 2px solid grey;
      background: #32CC99;
      width: 15px;
      margin: 22px 0 22px 0;
  }
  QScrollBar::handle:vertical {
      background: white;
      min-height: 20px;
  }
  QScrollBar::add-line:vertical {
      border: 2px solid grey;
      background: #32CC99;
      height: 20px;
      subcontrol-position: bottom;
      subcontrol-origin: margin;
  }

  QScrollBar::sub-line:vertical {
      border: 2px solid grey;
      background: #32CC99;
      height: 20px;
      subcontrol-position: top;
      subcontrol-origin: margin;
  }
  QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {
      border: 2px solid grey;
      width: 3px;
      height: 3px;
      background: white;
  }

  QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
      background: none;
  }</pre>
<a name="customizing-qsizegrip"></a>
<h3>Customizing QSizeGrip</h3>
<p><a href="qsizegrip.html">QSizeGrip</a> is usually styled by just setting an image.</p>
<pre> QSizeGrip {
     image: url(:/images/sizegrip.png);
     width: 16px;
     height: 16px;
 }</pre>
<a name="customizing-qslider"></a>
<h3>Customizing QSlider</h3>
<p>You can style horizontal slider as below:</p>
<pre> QSlider::groove:horizontal {
     border: 1px solid #999999;
     height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
     background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
     margin: 2px 0;
 }

 QSlider::handle:horizontal {
     background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
     border: 1px solid #5c5c5c;
     width: 18px;
     margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
     border-radius: 3px;
 }</pre>
<p>If you want to change the color of the slider parts before and after the handle, you can use the add-page and sub-page subcontrols. For example, for a vertical slider:</p>
<pre> QSlider::groove:vertical {
     background: red;
     position: absolute; /* absolutely position 4px from the left and right of the widget. setting margins on the widget should work too... */
     left: 4px; right: 4px;
 }

 QSlider::handle:vertical {
     height: 10px;
     background: green;
     margin: 0 -4px; /* expand outside the groove */
 }

 QSlider::add-page:vertical {
     background: white;
 }

 QSlider::sub-page:vertical {
     background: pink;
 }</pre>
<a name="customizing-qspinbox"></a>
<h3>Customizing QSpinBox</h3>
<p><a href="qspinbox.html">QSpinBox</a> can be completely customized as below (the style sheet has commentary inline):</p>
<pre> QSpinBox {
     padding-right: 15px; /* make room for the arrows */
     border-image: url(:/images/frame.png) 4;
     border-width: 3;
 }

 QSpinBox::up-button {
     subcontrol-origin: border;
     subcontrol-position: top right; /* position at the top right corner */

     width: 16px; /* 16 + 2*1px border-width = 15px padding + 3px parent border */
     border-image: url(:/images/spinup.png) 1;
     border-width: 1px;
 }

 QSpinBox::up-button:hover {
     border-image: url(:/images/spinup_hover.png) 1;
 }

 QSpinBox::up-button:pressed {
     border-image: url(:/images/spinup_pressed.png) 1;
 }

 QSpinBox::up-arrow {
     image: url(:/images/up_arrow.png);
     width: 7px;
     height: 7px;
 }

 QSpinBox::up-arrow:disabled, QSpinBox::up-arrow:off { /* off state when value is max */
    image: url(:/images/up_arrow_disabled.png);
 }

 QSpinBox::down-button {
     subcontrol-origin: border;
     subcontrol-position: bottom right; /* position at bottom right corner */

     width: 16px;
     border-image: url(:/images/spindown.png) 1;
     border-width: 1px;
     border-top-width: 0;
 }

 QSpinBox::down-button:hover {
     border-image: url(:/images/spindown_hover.png) 1;
 }

 QSpinBox::down-button:pressed {
     border-image: url(:/images/spindown_pressed.png) 1;
 }

 QSpinBox::down-arrow {
     image: url(:/images/down_arrow.png);
     width: 7px;
     height: 7px;
 }

 QSpinBox::down-arrow:disabled,
 QSpinBox::down-arrow:off { /* off state when value in min */
    image: url(:/images/down_arrow_disabled.png);
 }</pre>
<a name="customizing-qsplitter"></a>
<h3>Customizing QSplitter</h3>
<p>A <a href="qsplitter.html">QSplitter</a> derives from a <a href="qframe.html">QFrame</a> and hence can be styled like a <a href="qframe.html">QFrame</a>. The grip or the handle is customized using the <a href="stylesheet-reference.html#handle-sub">::handle</a> subcontrol.</p>
<pre> QSplitter::handle {
     image: url(images/splitter.png);
 }

 QSplitter::handle:horizontal {
     width: 2px;
 }

 QSplitter::handle:vertical {
     height: 2px;
 }

 QSplitter::handle:pressed {
     url(images/splitter_pressed.png);
 }</pre>
<a name="customizing-qstatusbar"></a>
<h3>Customizing QStatusBar</h3>
<p>We can provide a background for the status bar and a border for items inside the status bar as follows:</p>
<pre> QStatusBar {
     background: brown;
 }

 QStatusBar::item {
     border: 1px solid red;
     border-radius: 3px;
 }</pre>
<p>Note that widgets that have been added to the <a href="qstatusbar.html">QStatusBar</a> can be styled using the descendant declaration (i.e)</p>
<pre> QStatusBar QLabel {
     border: 3px solid white;
 }</pre>
<a name="customizing-qtabwidget-and-qtabbar"></a>
<h3>Customizing QTabWidget and QTabBar</h3>
<p align="center"><img src="images/tabWidget-stylesheet1.png" /></p><p>For the screenshot above, we need a stylesheet as follows:</p>
<pre> QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
 }

 QTabWidget::tab-bar {
     left: 5px; /* move to the right by 5px */
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

 QTabBar::tab:!selected {
     margin-top: 2px; /* make non-selected tabs look smaller */
 }</pre>
<p>Often we require the tabs to overlap to look like below:</p>
<p align="center"><img src="images/tabWidget-stylesheet2.png" /></p><p>For a tab widget that looks like above, we make use of <a href="http://www.communitymx.com/content/article.cfm?cid=B0029">negative margins</a>. The resulting stylesheet looks like this:</p>
<pre> QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
 }

 QTabWidget::tab-bar {
     left: 5px; /* move to the right by 5px */
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

 QTabBar::tab:!selected {
     margin-top: 2px; /* make non-selected tabs look smaller */
 }

 /* make use of negative margins for overlapping tabs */
 QTabBar::tab:selected {
     /* expand/overlap to the left and right by 4px */
     margin-left: -4px;
     margin-right: -4px;
 }

 QTabBar::tab:first:selected {
     margin-left: 0; /* the first selected tab has nothing to overlap with on the left */
 }

 QTabBar::tab:last:selected {
     margin-right: 0; /* the last selected tab has nothing to overlap with on the right */
 }

 QTabBar::tab:only-one {
     margin: 0; /* if there is only one tab, we don't want overlapping margins */
 }</pre>
<p>To move the tab bar to the center (as below), we require the following stylesheet:</p>
<p align="center"><img src="images/tabWidget-stylesheet3.png" /></p><pre> QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
     position: absolute;
     top: -0.5em;
 }

 QTabWidget::tab-bar {
     alignment: center;
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }</pre>
<p>The tear indicator and the scroll buttons can be further customized as follows:</p>
<pre> QTabBar::tear {
     image: url(tear_indicator.png);
 }

 QTabBar::scroller { /* the width of the scroll buttons */
     width: 20px;
 }

 QTabBar QToolButton { /* the scroll buttons are tool buttons */
     border-image: url(scrollbutton.png) 2;
     border-width: 2px;
 }

 QTabBar QToolButton::right-arrow { /* the arrow mark in the tool buttons */
     image: url(rightarrow.png);
 }

 QTabBar QToolButton::left-arrow {
     image: url(leftarrow.png);
 }</pre>
<p>Since Qt 4.6 the close button can be customized as follow:</p>
<pre> QTabBar::close-button {
     image: url(close.png)
     subcontrol-position: left;
 }
 QTabBar::close-button:hover {
     image: url(close-hover.png)
 }</pre>
<a name="customizing-qtableview"></a>
<h3>Customizing QTableView</h3>
<p>Suppose we'd like our selected item in <a href="qtableview.html">QTableView</a> to have bubblegum pink fade to white as its background.</p>
<p align="center"><img src="images/tableWidget-stylesheet.png" /></p><p>This is possible with the <a href="stylesheet-reference.html#selection-background-color-prop">selection-background-color</a> property and the syntax required is:</p>
<pre> QTableView {
     selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0.5, y2: 0.5,
                                 stop: 0 #FF92BB, stop: 1 white);
 }</pre>
<p>The corner widget can be customized using the following style sheet</p>
<pre> QTableView QTableCornerButton::section {
     background: red;
     border: 2px outset red;
 }</pre>
<a name="customizing-qtoolbar"></a>
<h3>Customizing QToolBar</h3>
<p>The background and the handle of a <a href="qtoolbar.html">QToolBar</a> is customized as below:</p>
<pre> QToolBar {
     background: red;
     spacing: 3px; /* spacing between items in the tool bar */
 }

 QToolBar::handle {
     image: url(handle.png);
 }</pre>
<a name="customizing-qtoolbox"></a>
<h3>Customizing QToolBox</h3>
<p>The tabs of the <a href="qtoolbox.html">QToolBox</a> are customized using the 'tab' subcontrol.</p>
<pre> QToolBox::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border-radius: 5px;
     color: darkgray;
 }

 QToolBox::tab:selected { /* italicize selected tabs */
     font: italic;
     color: white;
 }</pre>
<a name="customizing-qtoolbutton"></a>
<h3>Customizing QToolButton</h3>
<p>There are three types of QToolButtons.</p>
<ul>
<li>The <a href="qtoolbutton.html">QToolButton</a> has no menu. In this case, the <a href="qtoolbutton.html">QToolButton</a> is styled exactly like <a href="qpushbutton.html">QPushButton</a>. See <a href="#customizing-qpushbutton">Customizing QPushButton</a> for an example.</li>
<li>The <a href="qtoolbutton.html">QToolButton</a> has a menu and has the <a href="qtoolbutton.html#popupMode-prop">QToolButton::popupMode</a> set to <a href="qtoolbutton.html#ToolButtonPopupMode-enum">QToolButton::DelayedPopup</a> or <a href="qtoolbutton.html#ToolButtonPopupMode-enum">QToolButton::InstantPopup</a>. In this case, the <a href="qtoolbutton.html">QToolButton</a> is styled exactly like a <a href="qpushbutton.html">QPushButton</a> with a menu. See <a href="#customizing-qpushbutton">Customizing QPushButton</a> for an example of the usage of the menu-indicator pseudo state.</li>
<li>The <a href="qtoolbutton.html">QToolButton</a> has its <a href="qtoolbutton.html#popupMode-prop">QToolButton::popupMode</a> set to <a href="qtoolbutton.html#ToolButtonPopupMode-enum">QToolButton::MenuButtonPopup</a>. In this case, we style it as follows:</li>
</ul>
<pre> QToolButton { /* all types of tool button */
     border: 2px solid #8f8f91;
     border-radius: 6px;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #f6f7fa, stop: 1 #dadbde);
 }

 QToolButton[popupMode=&quot;1&quot;] { /* only for MenuButtonPopup */
     padding-right: 20px; /* make way for the popup button */
 }

 QToolButton:pressed {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #dadbde, stop: 1 #f6f7fa);
 }

 /* the subcontrols below are used only in the MenuButtonPopup mode */
 QToolButton::menu-button {
     border: 2px solid gray;
     border-top-right-radius: 6px;
     border-bottom-right-radius: 6px;
     /* 16px width + 4px for border = 20px allocated above */
     width: 16px;
 }

 QToolButton::menu-arrow {
     image: url(downarrow.png);
 }

 QToolButton::menu-arrow:open {
     top: 1px; left: 1px; /* shift it a bit */
 }</pre>
<a name="customizing-qtooltip"></a>
<h3>Customizing QToolTip</h3>
<p><a href="qtooltip.html">QToolTip</a> is customized exactly like a <a href="qlabel.html">QLabel</a>. In addition, for platforms that support it, the opacity property may be set to adjust the opacity.</p>
<p>For example,</p>
<pre> QToolTip {
     border: 2px solid darkkhaki;
     padding: 5px;
     border-radius: 3px;
     opacity: 200;
 }</pre>
<a name="customizing-qtreeview"></a>
<h3>Customizing QTreeView</h3>
<p>The background color of alternating rows can be customized using the following style sheet:</p>
<pre> QTreeView {
     alternate-background-color: yellow;
 }</pre>
<p>To provide a special background when you hover over items, we can use the <a href="stylesheet-reference.html#item-sub">::item</a> subcontrol. For example,</p>
<pre> QTreeView {
     show-decoration-selected: 1;
 }

 QTreeView::item {
      border: 1px solid #d9d9d9;
     border-top-color: transparent;
     border-bottom-color: transparent;
 }

 QTreeView::item:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
     border: 1px solid #bfcde4;
 }

 QTreeView::item:selected {
     border: 1px solid #567dbc;
 }

 QTreeView::item:selected:active{
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
 }

 QTreeView::item:selected:!active {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
 }</pre>
<p>The branches of a <a href="qtreeview.html">QTreeView</a> are styled using the <a href="stylesheet-reference.html#branch-sub">::branch</a> subcontrol. The following stylesheet color codes the various states when drawing a branch.</p>
<pre> QTreeView::branch {
         background: palette(base);
 }

 QTreeView::branch:has-siblings:!adjoins-item {
         background: cyan;
 }

 QTreeView::branch:has-siblings:adjoins-item {
         background: red;
 }

 QTreeView::branch:!has-children:!has-siblings:adjoins-item {
         background: blue;
 }

 QTreeView::branch:closed:has-children:has-siblings {
         background: pink;
 }

 QTreeView::branch:has-children:!has-siblings:closed {
         background: gray;
 }

 QTreeView::branch:open:has-children:has-siblings {
         background: magenta;
 }

 QTreeView::branch:open:has-children:!has-siblings {
         background: green;
 }</pre>
<p>Colorful, though it is, a more useful example can be made using the following images:</p>
<p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><img src="images/stylesheet-vline.png" /></td><td><img src="images/stylesheet-branch-more.png" /></td><td><img src="images/stylesheet-branch-end.png" /></td><td><img src="images/stylesheet-branch-closed.png" /></td><td><img src="images/stylesheet-branch-open.png" /></td></tr>
<tr valign="top" class="even"><td>vline.png</td><td>branch-more.png</td><td>branch-end.png</td><td>branch-closed.png</td><td>branch-open.png</td></tr>
</table></p>
<pre> QTreeView::branch:has-siblings:!adjoins-item {
     border-image: url(vline.png) 0;
 }

 QTreeView::branch:has-siblings:adjoins-item {
     border-image: url(branch-more.png) 0;
 }

 QTreeView::branch:!has-children:!has-siblings:adjoins-item {
     border-image: url(branch-end.png) 0;
 }

 QTreeView::branch:has-children:!has-siblings:closed,
 QTreeView::branch:closed:has-children:has-siblings {
         border-image: none;
         image: url(branch-closed.png);
 }

 QTreeView::branch:open:has-children:!has-siblings,
 QTreeView::branch:open:has-children:has-siblings  {
         border-image: none;
         image: url(branch-open.png);
 }</pre>
<p>The resulting tree view looks like this:</p>
<p align="center"><img src="images/stylesheet-treeview.png" /></p><a name="common-mistakes"></a>
<h2>Common mistakes</h2>
<p>This section lists some common mistakes when using stylesheets.</p>
<a name="qpushbutton-and-images"></a>
<h3>QPushButton and images</h3>
<p>When styling a <a href="qpushbutton.html">QPushButton</a>, it is often desirable to use an image as the button graphic. It is common to try the <a href="stylesheet-reference.html#background-image-prop">background-image</a> property, but this has a number of drawbacks: For instance, the background will often appear hidden behind the button decoration, because it is not considered a background. In addition, if the button is resized, the entire background will be stretched or tiled, which does not always look good.</p>
<p>It is better to use the <a href="stylesheet-reference.html#border-image-prop">border-image</a> property, as it will always display the image, regardless of the background (you can combine it with a background if it has alpha values in it), and it has special settings to deal with button resizing.</p>
<p>Consider the following snippet:</p>
<pre>         QPushButton {
             color: grey;
             border-image: url(/home/kamlie/code/button.png) 3 10 3 10;
             border-top: 3px transparent;
             border-bottom: 3px transparent;
             border-right: 10px transparent;
             border-left: 10px transparent;
         }</pre>
<p>This will produce a button looking like this:</p>
<p align="center"><img src="images/stylesheet-border-image-normal.png" /></p><p>The numbers after the url gives the top, right, bottom and left number of pixels, respectively. These numbers correspond to the border and should not stretch when the size changes. Whenever you resize the button, the middle part of the image will stretch in both directions, while the pixels specified in the stylesheet will not. This makes the borders of the button look more natural, like this:</p>
<p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><img src="images/stylesheet-border-image-stretched.png" /></td></tr>
<tr valign="top" class="even"><td>With borders</td></tr>
</table></p>
<p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><img src="images/stylesheet-border-image-wrong.png" /></td></tr>
<tr valign="top" class="even"><td>Without borders</td></tr>
</table></p>
<p>See also <a href="widgets-stylesheet.html">Style Sheet Example</a>, <a href="richtext-html-subset.html">Supported HTML Subset</a>, and <a href="qstyle.html">QStyle</a>.</p>
<p>
[Previous: <a href="stylesheet-reference.html">Qt Style Sheets Reference</a>]
[<a href="stylesheet.html">Contents</a>]
</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>