1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- stylesheet.qdoc -->
<title>Qt 4.8: Qt Style Sheets Examples</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li>Qt Style Sheets Examples</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="stylesheet-reference.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="stylesheet-reference.html">Qt Style Sheets Reference</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#style-sheet-usage">Style Sheet Usage</a></li>
<li class="level2"><a href="#customizing-the-foreground-and-background-colors">Customizing the Foreground and Background Colors</a></li>
<li class="level2"><a href="#customizing-using-dynamic-properties">Customizing Using Dynamic Properties</a></li>
<li class="level2"><a href="#customizing-a-qpushbutton-using-the-box-model">Customizing a QPushButton Using the Box Model</a></li>
<li class="level2"><a href="#customizing-the-qpushbutton-s-menu-indicator-sub-control">Customizing the QPushButton's Menu Indicator Sub-Control</a></li>
<li class="level2"><a href="#complex-selector-example">Complex Selector Example</a></li>
<li class="level1"><a href="#customizing-specific-widgets">Customizing specific widgets</a></li>
<li class="level2"><a href="#customizing-qabstractscrollarea">Customizing QAbstractScrollArea</a></li>
<li class="level2"><a href="#customizing-qcheckbox">Customizing QCheckBox</a></li>
<li class="level2"><a href="#customizing-qcombobox">Customizing QComboBox</a></li>
<li class="level2"><a href="#customizing-qdockwidget">Customizing QDockWidget</a></li>
<li class="level2"><a href="#customizing-qframe">Customizing QFrame</a></li>
<li class="level2"><a href="#customizing-qgroupbox">Customizing QGroupBox</a></li>
<li class="level2"><a href="#customizing-qheaderview">Customizing QHeaderView</a></li>
<li class="level2"><a href="#customizing-qlineedit">Customizing QLineEdit</a></li>
<li class="level2"><a href="#customizing-qlistview">Customizing QListView</a></li>
<li class="level2"><a href="#customizing-qmainwindow">Customizing QMainWindow</a></li>
<li class="level2"><a href="#customizing-qmenu">Customizing QMenu</a></li>
<li class="level2"><a href="#customizing-qmenubar">Customizing QMenuBar</a></li>
<li class="level2"><a href="#customizing-qprogressbar">Customizing QProgressBar</a></li>
<li class="level2"><a href="#customizing-qpushbutton">Customizing QPushButton</a></li>
<li class="level2"><a href="#customizing-qradiobutton">Customizing QRadioButton</a></li>
<li class="level2"><a href="#customizing-qscrollbar">Customizing QScrollBar</a></li>
<li class="level2"><a href="#customizing-qsizegrip">Customizing QSizeGrip</a></li>
<li class="level2"><a href="#customizing-qslider">Customizing QSlider</a></li>
<li class="level2"><a href="#customizing-qspinbox">Customizing QSpinBox</a></li>
<li class="level2"><a href="#customizing-qsplitter">Customizing QSplitter</a></li>
<li class="level2"><a href="#customizing-qstatusbar">Customizing QStatusBar</a></li>
<li class="level2"><a href="#customizing-qtabwidget-and-qtabbar">Customizing QTabWidget and QTabBar</a></li>
<li class="level2"><a href="#customizing-qtableview">Customizing QTableView</a></li>
<li class="level2"><a href="#customizing-qtoolbar">Customizing QToolBar</a></li>
<li class="level2"><a href="#customizing-qtoolbox">Customizing QToolBox</a></li>
<li class="level2"><a href="#customizing-qtoolbutton">Customizing QToolButton</a></li>
<li class="level2"><a href="#customizing-qtooltip">Customizing QToolTip</a></li>
<li class="level2"><a href="#customizing-qtreeview">Customizing QTreeView</a></li>
<li class="level1"><a href="#common-mistakes">Common mistakes</a></li>
<li class="level2"><a href="#qpushbutton-and-images">QPushButton and images</a></li>
</ul>
</div>
<h1 class="title">Qt Style Sheets Examples</h1>
<span class="subtitle"></span>
<!-- $$$stylesheet-examples.html-description -->
<div class="descr"> <a name="details"></a>
<p>We will now see a few examples to get started with using Qt Style Sheets.</p>
<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 class="cpp"> qApp<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"QLineEdit { background-color: yellow }"</span>);</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 class="cpp"> myDialog<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"QLineEdit { background-color: yellow }"</span>);</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 class="cpp"> myDialog<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"QLineEdit#nameEdit { background-color: yellow }"</span>);</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 class="cpp"> nameEdit<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"background-color: yellow"</span>);</pre>
<p>To ensure a good contrast, we should also specify a suitable color for the text:</p>
<pre class="cpp"> nameEdit<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"color: blue; background-color: yellow"</span>);</pre>
<p>It might be a good idea to change the colors used for selected text as well:</p>
<pre class="cpp"> nameEdit<span class="operator">-</span><span class="operator">></span>setStyleSheet(<span class="string">"color: blue;"</span>
<span class="string">"background-color: yellow;"</span>
<span class="string">"selection-color: yellow;"</span>
<span class="string">"selection-background-color: blue;"</span>);</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 class="cpp"> *[mandatoryField="true"] { 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 class="cpp"> <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>nameEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="keyword">this</span>);
nameEdit<span class="operator">-</span><span class="operator">></span>setProperty(<span class="string">"mandatoryField"</span><span class="operator">,</span> <span class="keyword">true</span>);
<span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>emailEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="keyword">this</span>);
emailEdit<span class="operator">-</span><span class="operator">></span>setProperty(<span class="string">"mandatoryField"</span><span class="operator">,</span> <span class="keyword">true</span>);
<span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>ageSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>(<span class="keyword">this</span>);
ageSpinBox<span class="operator">-</span><span class="operator">></span>setProperty(<span class="string">"mandatoryField"</span><span class="operator">,</span> <span class="keyword">true</span>);</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 class="cpp"> QPushButton#evilButton { background-color: red }</pre>
<p>However, the result is a boring, flat button with no borders:</p>
<p class="centerAlign"><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., 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 class="cpp"> QPushButton#evilButton {
background-color: red;
border-style: outset;
border-width: 2px;
border-color: beige;
}</pre>
<p class="centerAlign"><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 class="cpp"> 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 class="centerAlign"><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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> QLineEdit { color: red }
QLineEdit[readOnly="true"] { 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 class="cpp"> QLineEdit { color: red }
QLineEdit[readOnly="true"] { 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 class="cpp"> QLineEdit { color: red }
QLineEdit[readOnly="true"] { 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 "merged" with the combo box frame.</p>
<pre class="cpp"> 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 "on" 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> QLineEdit[echoMode="2"] {
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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="centerAlign"><img src="images/progressBar-stylesheet.png" alt="" /></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 class="cpp"> 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 class="cpp"> 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 class="centerAlign"><img src="images/progressBar2-stylesheet.png" alt="" /></p><p>In the screenshot above, we use a <a href="stylesheet-reference.html#margin-prop">margin</a> of 0.5 pixels.</p>
<pre class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="centerAlign"><img src="images/stylesheet-scrollbar1.png" alt="" /></p><p>The scroll bar above has been styled in aquamarine with a solid grey border.</p>
<pre class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="centerAlign"><img src="images/stylesheet-scrollbar2.png" alt="" /></p><p>To customize a vertical scroll bar use a style sheet similar to the following:</p>
<pre class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> QSplitter::handle {
image: url(images/splitter.png);
}
QSplitter::handle:horizontal {
width: 2px;
}
QSplitter::handle:vertical {
height: 2px;
}
QSplitter::handle:pressed {
image: 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 class="cpp"> 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 class="cpp"> QStatusBar QLabel {
border: 3px solid white;
}</pre>
<a name="customizing-qtabwidget-and-qtabbar"></a>
<h3>Customizing QTabWidget and QTabBar</h3>
<p class="centerAlign"><img src="images/tabWidget-stylesheet1.png" alt="" /></p><p>For the screenshot above, we need a stylesheet as follows:</p>
<pre class="cpp"> 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 class="centerAlign"><img src="images/tabWidget-stylesheet2.png" alt="" /></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 class="cpp"> 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 class="centerAlign"><img src="images/tabWidget-stylesheet3.png" alt="" /></p><pre class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="centerAlign"><img src="images/tableWidget-stylesheet.png" alt="" /></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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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="1"] { /* 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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 class="cpp"> 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>
<table class="generic">
<tr valign="top" class="odd"><td ><img src="images/stylesheet-vline.png" alt="" /></td><td ><img src="images/stylesheet-branch-more.png" alt="" /></td><td ><img src="images/stylesheet-branch-end.png" alt="" /></td><td ><img src="images/stylesheet-branch-closed.png" alt="" /></td><td ><img src="images/stylesheet-branch-open.png" alt="" /></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>
<pre class="cpp"> 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 class="centerAlign"><img src="images/stylesheet-treeview.png" alt="" /></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 class="cpp"> <span class="type"><a href="qpushbutton.html">QPushButton</a></span> {
color: grey;
border<span class="operator">-</span>image: url(<span class="operator">/</span>home<span class="operator">/</span>kamlie<span class="operator">/</span>code<span class="operator">/</span>button<span class="operator">.</span>png) <span class="number">3</span> <span class="number">10</span> <span class="number">3</span> <span class="number">10</span>;
border<span class="operator">-</span>top: <span class="number">3px</span> transparent;
border<span class="operator">-</span>bottom: <span class="number">3px</span> transparent;
border<span class="operator">-</span>right: <span class="number">10px</span> transparent;
border<span class="operator">-</span>left: <span class="number">10px</span> transparent;
}</pre>
<p>This will produce a button looking like this:</p>
<p class="centerAlign"><img src="images/stylesheet-border-image-normal.png" alt="" /></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>
<table class="generic">
<tr valign="top" class="odd"><td ><img src="images/stylesheet-border-image-stretched.png" alt="" /></td></tr>
<tr valign="top" class="even"><td >With borders</td></tr>
</table>
<table class="generic">
<tr valign="top" class="odd"><td ><img src="images/stylesheet-border-image-wrong.png" alt="" /></td></tr>
<tr valign="top" class="even"><td >Without borders</td></tr>
</table>
</div>
<p><b>See also </b><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>
<!-- @@@stylesheet-examples.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="stylesheet-reference.html">Qt Style Sheets Reference</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|