有關於QT樣式表的一些知識(使用方法和編寫語法)
一、QT樣式表的介紹
QT樣式表文件.qss類似於層疊式樣式表,樣式表文件能夠減少專案中源原件的程式碼量,原始碼中不必再對每個控制元件的樣式進行設定。而且樣式表是一個獨立的檔案,其非常便於修改,對大型工程專案的統一介面風格設計和修改都很便利。
本文僅對樣式表的基礎語法做一個簡單的介紹,由於受實際使用時的專案經驗限制,不是很全面。另,文後附一個使用例項,可供參考。
二、使用方法
- 先建立文字檔案,書寫樣式表,編寫完畢後重命名為.qss檔案
- 在專案中新增Qt資原始檔.qrc檔案。如source.qrc,把樣式表文件加入到資原始檔中,此處注意prefix最好為"/",否則在呼叫qss檔案時會找不到檔案。例如在專案資料夾下建立一個source資料夾,把所有樣式表用到的圖片都放入這個目錄下,樣式表文件和資原始檔也都放在這個目錄下。現在在資原始檔中加入qss檔案路徑:
<RCC>
<qresource prefix="/">
<file>arrow_down_normal.png</file>
... ...
<file>source.qss</file>
... ...
</qresource>
</RCC>
3.在main函式中通過傳入路徑\檔名的方式建立一個QFile物件,以readonly的方式開啟,然後readAll,最後
qApp->setStyleSheet就可以使qss生效。
程式碼如下:
QApplication app(argc, argv);
QFile qss("source.qss");
qss.open(QFile::ReadOnly);
app.setStyleSheet(qss.readAll());
qss.close();
三、基礎語法
1. 一個樣式表由一系列的樣式規則構成。每個樣式規則都如下形式:
selector { attribute: value }
selector部分通常是一個類名(例如QComboBox),也就是一種部件的名稱;屬性(attribute)部分是一個樣式表屬性的名字(如背景色,邊框大小,字型等),值(value)部分是賦給該屬性的值(顏色,大小等的值)。
也可以同時給多個部件定義,如下:
selector1, selector2, ..., selectorM {
attribute1: value1;
attribute2: value2;
...
attributeN: valueN;
}
2. 另外介紹一下方箱模型
在樣式表中,每個部件都被看作是一個由四個同心相似的矩形組成的箱體:空白(margin)、邊框(border)、填充(padding)和內容(content)。對於一個平面部件——例如一個空白、邊框和填充都是0畫素的部件——而言,這四個矩形是完全重合的。
空白區域位於邊框外,並且總是透明的。邊框為部件提供了四周的框架,其border-style屬性可以設定為一些內建的框架風格,如inset、outset、solid和ridge。填充在邊框和內容區域之間提供了空白間隔。
3. 偽狀態的設定
定義介面元素不同的狀態所展現的樣式稱為“偽狀態樣式”。偽狀態和部件使用:來表示。偽狀態的書寫格式為:
Selector:狀態 { attribute: value }
例如:
QPushButton:disabled {
padding-left: 4px;
padding-top: 4px;
background-color: gray;
}
偽狀態主要有:
:checked button部件被選中
:disabled 部件被禁用
:enabled 部件被啟用
:focus 部件獲得焦點
:hover 滑鼠位於部件上
:indeterminate checkbox或radiobutton被部分選中
:off 部件可以切換,且處於off狀態
:on 部件可以切換,且處於on狀態
:pressed 部件被滑鼠按下
:unchecked button部件未被選中
4.子部件的設定
許多部件都有子部件,如QComboBox的下拉箭頭,子部件和主部件使用::來表示。格式為:
Selector::子部件{ attribute: value }
如果還要用到偽狀態則:Selector::子部件:偽狀態{ attribute: value }
子部件有:
::down-arrow combo box或spin box的下拉箭頭
::down-button spin box的向下按鈕
::drop-down combo box的下拉箭頭
::indicator checkbox、radio button或可選擇group box的指示器
::item menu、menu bar或status bar的子專案
::menu-indicator push button的選單指示器
::title group box的標題
::up-arrow spin box的向上箭頭
::up-button spin box的向上按鈕
5.其他
qss的註釋支援" /* */",不支援“//",如果使用了"//"進行註釋,可能導致該符號後面的樣式表設定內容全部失效。
四、一個樣式表例項
QDialog {
border: 2px solid #5BBDBE; /*邊框:2px,實線,色號*/
/*border-radius: 6px;*/
}
/*視窗中的可滾動區域*/
QScrollArea {
border: 2px solid #5BBDBE;
border-radius: 4px; /*邊框半徑用於實現圓角矩形樣式*/
}
QGroupBox {
border: 2px solid #5BBDBE;
border-radius: 5px;
margin-top: 1ex; /* leave space at the top for the title */
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left; /* position at the top center */
left: 20px;
}
QTabWidget::pane { /* The tab widget frame */
margin: 0px;
border-left: 2px solid #5BBDBE;
border-bottom: 2px solid #5BBDBE;
border-top: 2px solid #5BBDBE;
border-right: 2px solid #5BBDBE;
border-radius: 4px;
position: absolute;
top: -2px;
}
QTabWidget::tab-bar {
left: 0px; /* move to the right by 5px */
}
/* Style the tab using the tab sub-control. Note that
it reads QTabBar _not_ QTabWidget */
QTabBar::tab {
color: #FFFFFF;
background: #018E88;
border: 2px solid #5BBDBE;
border-bottom-color: #5BBDBE; /* same as the pane color */
border-top-left-radius: 10px;
border-top-right-radius: 10px;
min-width: 8ex;
padding: 2px;
}
/*滑鼠懸浮tab頁面上*/
QTabBar::tab:hover {
color: #018E88; /*前景色*/
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #DDEDED, stop: 1.0 #EEF6F6);
} /*背景色為漸變色,從stop:0至stop:1.0漸變*/
/*被選擇的tab頁面*/
QTabBar::tab:selected {
color: #018E88;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #DDEDED, stop: 1.0 #EEF6F6);
border-color: #5BBDBE;
border-bottom-color: #EEF6F6; /* same as pane color */
}
/*未被選的tab頁面*/
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: -2px;
margin-right: -2px;
}
QTabBar::tab:first {
margin-left: 30;
}
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 */
}
QPushButton {
/*border: 2px solid #30A29F*/;
border: none;
padding: 2px 2px 2px 2px;
border-radius: 6px;
color: #FFFFFF;
background-color: #30A29F;
min-width: 70px;
min-height: 25px;
}
QPushButton:disabled {
padding-left: 4px;
padding-top: 4px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #EEF6F6, stop: 1 #000000);
}
QPushButton:pressed {
padding-left: 4px;
padding-top: 4px;
color: #30A29F;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #EEF6F6, stop: 1 #FFFFFF);
}
QPushButton:hover {
color: #30A29F;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #BEDDDE, stop: 1 #E7F2F2);
}
QPushButton:flat {
border: none; /* no border for a flat push button */
}
QPushButton:default {
border-color: navy; /* make the default button prominent */
}
QTreeView {
border: 1px solid #5BBDBE;
show-decoration-selected: 1;
background-color:#FFFFFF;
selection-color: #000000;
selection-background-color:#FFFFFF;
font-size: 12pt;
}
QTreeView::item {
border: 1px solid #FFFFFF;
border-top-color: transparent;
border-bottom-color: transparent;
}
QTreeView::item:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #DDEDED, stop: 1 #EEF6F6);
border: 1px solid #BEDDDE;
}
QTreeView::item:selected {
border: 1px solid #BEDDDE;
}
QTreeView::item:selected:active{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #BEDDDE, stop: 1 #E7F2F2);
}
QTreeView::item:selected:!active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #BEDDDE, stop: 1 #E7F2F2);
}
/*表格樣式*/
QTableView {
border: 1px solid #5BBDBE;
show-decoration-selected: 1;
background-color:#F4F4F4;
alternate-background-color:#EAEAEA; /*行顏色交替變化*/
selection-color: #000000;
selection-background-color:#BEDDDE;
font-size: 14pt;
}
QTableView::item {
border: 1px solid #FFFFFF;
border-top-color: transparent;
border-bottom-color: transparent;
}
QTableView::item:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #DDEDED, stop: 1 #EEF6F6);
border: 1px solid #BEDDDE;
}
QTableView::item:selected {
color: #018E88;
border: 1px solid #BEDDDE;
}
QTableView::item:selected:active{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #BEDDDE, stop: 1 #E7F2F2);
}
QTableView::item:selected:!active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #BEDDDE, stop: 1 #E7F2F2);
}
QTableView QTableCornerButton::section {
border: 0px;
background-color: #8D8D8D;
}
QHeaderView::section {
background-color: #018E88;
color: white;
font-size: 14pt;
min-width: 25px;
min-height: 25px;
padding-left: 0px;
border: 0px solid #018E88;
}
QHeaderView::section:checked
{
background-color: #018E88;
}
/* style the sort indicator */
QHeaderView::down-arrow {
image: url(./source/arrow_down_normal.png);
}
QHeaderView::down-arrow:hover {
image: url(./source/arrow_down_press.png);
}
QHeaderView::up-arrow {
image: url(./source/arrow_up_normal.png);
}
QHeaderView::up-arrow:hover {
image: url(./source/arrow_up_press.png);
}
QScrollBar:horizontal {
border: 2px solid #018E88;
background: #018E88;
height: 20px;
margin: 0 22px 0 22px;
}
QScrollBar::handle:horizontal {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E5E5E5, stop: 1 #DADADA);
min-width: 20px;
}
QScrollBar::handle:horizontal:hover {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #DADADA, stop: 1 #018E88);
min-width: 20px;
}
QScrollBar::handle:horizontal:pressed {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #B8B8B8, stop: 1 #A3A3A3);
min-width: 20px;
}
QScrollBar::add-line:horizontal {
border: 2px solid #018E88;
background: #018E88;
width: 20px;
subcontrol-position: right;
subcontrol-origin: margin;
}
QScrollBar::sub-line:horizontal {
border: 2px solid #018E88;
background: #018E88;
width: 20px;
subcontrol-position: left;
subcontrol-origin: margin;
}
QScrollBar:left-arrow:horizontal, QScrollBar::right-arrow:horizontal {
border: 2px solid #018E88;
border-radius: 6px;
width: 10px;
height: 10px;
background: #E5E5E5;
}
QScrollBar:left-arrow:horizontal:hover, QScrollBar::right-arrow:horizontal:hover {
border: 2px solid #018E88;
border-radius: 6px;
width: 10px;
height: 10px;
background: #FFFFFF;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {
background: none;
}
QScrollBar:vertical {
border: 2px solid #018E88;
background: #018E88;
width: 20px;
margin: 22px 0 22px 0;
}
QScrollBar::handle:vertical {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #E5E5E5, stop: 1 #DADADA);
min-height: 20px;
}
QScrollBar::handle:vertical:hover {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #DADADA, stop: 1 #018E88);
min-height: 20px;
}
QScrollBar::handle:vertical:pressed {
border-radius: 6px;
background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #B8B8B8, stop: 1 #A3A3A3);
min-height: 20px;
}
QScrollBar::add-line:vertical {
border: 2px solid #018E88;
background: #018E88;
height: 20px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}
QScrollBar::sub-line:vertical {
border: 2px solid #018E88;
background: #018E88;
height: 20px;
subcontrol-position: top;
subcontrol-origin: margin;
}
QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {
border: 2px solid #018E88;
border-radius: 6px;
width: 10px;
height: 10px;
background: #E5E5E5;
}
QScrollBar::up-arrow:vertical:hover, QScrollBar::down-arrow:vertical:hover {
border: 2px solid #018E88;
border-radius: 6px;
width: 10px;
height: 10px;
background: #FFFFFF;
}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
background: none;
}
QComboBox:disabled {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #EEF6F6, stop: 1 #000000);
}
QComboBox {
border: 2px solid #018E88;
border-radius: 6px;
padding: 1px 1px 1px 1px;
min-height: 25px;
color: #FFFFFF;
background-color: #018E88;
}
QComboBox:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #01BFB6, stop: 1.0 #02E3D7);
}
QComboBox:on { /* shift the text when the popup opens */
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #01BFB6, stop: 1.0 #02E3D7);
padding-top: 3px;
padding-left: 3px;
}
QComboBox::drop-down {
subcontrol-origin: padding;
subcontrol-position: top right;
width: 20px;
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(./source/arrow_down_normal.png);
}
QComboBox::down-arrow:on { /* shift the arrow when popup is open */
image: url(./source/arrow_down_press.png);
top: 1px;
left: 1px;
}
QComboBox QAbstractItemView {
show-decoration-selected: 1;
background-color:#BEDDDE;
alternate-background-color:#BEDDDE;
selection-color: #000000;
selection-background-color:#BEDDDE;
outline: none;
font-size: 12px;
border: 2px solid #018E88;
}
QDateEdit:disabled {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #EEF6F6, stop: 1 #000000);
}
QDateEdit {
border: 2px solid #018E88;
border-radius: 6px;
padding: 1px 1px 1px 1px;
min-height: 25px;
color: #FFFFFF;
background-color: #018E88;
}
QDateEdit:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #01BFB6, stop: 1.0 #02E3D7);
}
QDateEdit:on { /* shift the text when the popup opens */
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #01BFB6, stop: 1.0 #02E3D7);
padding-top: 3px;
padding-left: 3px;
}
QDateEdit::drop-down {
subcontrol-origin: padding;
subcontrol-position: top right;
width: 20px;
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;
}
QDateEdit::down-arrow {
image: url(./source/arrow_down_normal.png);
}
QDateEdit::down-arrow:on { /* shift the arrow when popup is open */
image: url(./source/arrow_down_press.png);
top: 1px;
left: 1px;
}
QDateEdit QAbstractItemView {
show-decoration-selected: 1;
background-color:#BEDDDE;
alternate-background-color:#BEDDDE;
selection-color: #000000;
selection-background-color:#BEDDDE;
outline: none;
font-size: 12px;
border: 2px solid #018E88;
}
QLineEdit {
border: 2px solid #018E88;
border-radius: 10px;
padding: 1px 1px 1px 1px;
color: #000000;
background: #EAEAEA;
selection-color: #000000;
selection-background-color: #018E88;
min-height: 25px;
}
QTextEdit {
border: 2px solid #018E88;
border-radius: 10px;
padding: 1px 1px 1px 1px;
color: #000000;
background: #EAEAEA;
selection-color: #000000;
selection-background-color: #018E88;
min-height: 25px;
}
QCheckBox {
spacing: 5px;
}
QCheckBox::indicator {
width: 24px;
height: 24px;
}
QCheckBox::indicator:unchecked {
image: url(./source/checkbox_unchecked.png);/*使用自定義的影象*/
}
QCheckBox::indicator:unchecked:hover {
image: url(./source/checkbox_unchecked_hover.png);
}
QCheckBox::indicator:unchecked:pressed {
image: url(./source/checkbox_unchecked_pressed.png);
}
QCheckBox::indicator:checked {
image: url(./source/checkbox_checked.png);
}
QCheckBox::indicator:checked:hover {
image: url(./source/checkbox_checked_hover.png);
}
QCheckBox::indicator:checked:pressed {
image: url(./source/checkbox_checked_pressed.png);
}
/*
QCheckBox::indicator:indeterminate:hover {
image: url(./source/checkbox_indeterminate_hover.png);
}
QCheckBox::indicator:indeterminate:pressed {
image: url(./source/checkbox_indeterminate_pressed.png);
}
*/
QRadioButton {
spacing: 5px;
}
QRadioButton::indicator {
width: 24px;
height: 24px;
}
QRadioButton::indicator::unchecked {
image: url(./source/radiobutton_unchecked.png);
}
QRadioButton::indicator:unchecked:hover {
image: url(./source/radiobutton_unchecked_hover.png);
}
QRadioButton::indicator:unchecked:pressed {
image: url(./source/radiobutton_unchecked_pressed.png);
}
QRadioButton::indicator::checked {
image: url(./source/radiobutton_checked.png);
}
QRadioButton::indicator:checked:hover {
image: url(./source/radiobutton_checked_hover.png);
}
QRadioButton::indicator:checked:pressed {
image: url(./source/radiobutton_checked_pressed.png);
}