【QT】QT从零⼊门教程(⼗六):QSS样式表
实现⼀个软件的基本界⾯和功能后,接下来要做的就是美化界⾯,这就⽤到了QSS样式表。
QSS包含了⼀个样式规则序列,⼀个样式规则由⼀个选择器和声明组成,选择器指定哪些部件由规则影响,声明指定哪些属性应该在部件上进⾏设置。例如:
QPushButton { color: red }
其中QPushButton是选择器,{ color: red }是声明,该规则指定QPushButton及其⼦类(例如:MyPushButton)应使⽤红⾊作为前景⾊。QSS通常不区分⼤⼩写(即:color、Color、COLOR、cOloR指同⼀属性),唯⼀例外就是类名(class names)、对象名
(o bject names)、属性名(p roperty names)区分⼤⼩写。
⼏个选择器可以指定相同的声明,使⽤逗号(,)来分隔选择器。例如:
QPushButton, QLineEdit, QComboBox { color: red; background-color: white }
对于样式复杂的部件,需要访问⼦控件,例如:QComboBox的下拉按钮或QSpinBox的上下箭头。选择器可能包含⼦控件,使得可以限制特有部件⼦控件的应⽤规则。例如:
QComboBox::drop-down { image: url(dropdown.png) }
上述规则指定了QComboBoxe下拉按钮样式,虽然双冒号(::)语法让⼈想起CSS3伪元素,但Qt⼦控件从概念上讲有不同的级联语义。
当样式中指定相同的属性具有不同的值时,就会出现冲突。如下所⽰,okButton的特定属性和QPushButton 的通⽤属性不同,这时okButton所指定的属性则是color: gray。
QPushButton#okButton { color: gray }
QPushButton { color: red }
使⽤⽰例
QPushButton *button = new QPushButton();
button->setObjectName("okButton");
okButton->setStyleSheet("background-color:blue;"); //设置背景颜⾊
⿊⾊样式表QSS (这⾥引⽤了Github的)
/*
* TODO: Search buttons color (Replace, Replace and continue search)
*/
/*
* Drop background color of most widgets
*/
QWidget, QStackedWidget,
QScrollArea, QAbstractScrollArea{
background-color: transparent;
}
QApplication,
QMainWindow, /*QFrame, */QDockWidget, QDialog, QFileDialog, QMessageBox, QRadioButton,
QGroupBox::title, /* Need to set background becase must clear border behind label */ QMenuBar, QMenu, QMenu::item,
QComboBox QAbstractItemView,
QTreeView, QListWidget, QListView, QTableWidget, QTableView,
QTextEdit, QTextBrowser{
background-color:#3c3f41; /*#004100;*/
color:#bbbbbb;
}
/*
* This is default values
* Edit first if you want change appearance
*/
/****⾃定义*****/
QPushButton#customButton{
min-height: 1.2em;
min-width: 1.2em;
outline: 0;
border: 1px solid #5f6161;
border-radius: 2px;
background-color:rgba(100, 100, 100, 0.05);
}
/*
QFrame#customFrame {
border: 0px solid transparent;
background-color: transparent;
} */
QDialog{
border:1px solid #4b6eaf;
}
/****END*****/
QLabel, QTabBar::tab, QMenuBar::item,
QCheckBox, QGroupBox{
background-color: transparent;
color:#bbbbbb;
selection-color:#bbbbbb;
}
/*
* GroupBox and CheckBox
*
* TODO: ::indicator:indeterminate icons
*/
QGroupBox{
border-top: 1px solid #2d2d2d;
margin-top: 0.5em;
}
QGroupBox::title{
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 3px;
}
QCheckBox::indicator,
QGroupBox::indicator{
width: 13px;
width: 13px;
height: 13px;
}
QCheckBox::indicator:unchecked,
QGroupBox::indicator:unchecked{
image:url(:/qmldesigner/images/checkbox_unchecked.png);
}
QCheckBox::indicator:unchecked:hover,
QGroupBox::indicator:unchecked:hover{
image:url(:/qmldesigner/images/checkbox_unchecked_hover.png);
}
QCheckBox::indicator:unchecked:pressed,
QGroupBox::indicator:unchecked:pressed{
image:url(:/qmldesigner/images/checkbox_unchecked_pressed.png);
}
QCheckBox::indicator:checked,
QGroupBox::indicator:checked{
image:url(:/qmldesigner/images/checkbox_checked.png);
}
QCheckBox::indicator:checked:hover,
QGroupBox::indicator:checked:hover{
image:url(:/qmldesigner/images/checkbox_checked_hover.png);
}
QCheckBox::indicator:checked:pressed,
QGroupBox::indicator:checked:pressed{
image:url(:/qmldesigner/images/checkbox_checked_pressed.png);
}
QCheckBox::indicator:indeterminate:hover,
QGroupBox::indicator:indeterminate:hover{
background-color:#2222bb;
image:url(:/qmldesigner/images/checkbox_indeterminate_hover.png); /* TODO: There is no shuch image */ }
QCheckBox::indicator:indeterminate:pressed,
QGroupBox::indicator:indeterminate:pressed{
background-color:#22bb22;
image:url(:/qmldesigner/images/checkbox_indeterminate_pressed.png); /* TODO: There is no shuch image */ }
QLineEdit, QAbstractSpinBox, QComboBox, QPushButton{
background-color:#45494a;
/*background-color: rgba(255, 255, 255, 0.05);*/
background-origin: border;
css选择器分为哪几类border: 1px solid #646464;
color:#bbbbbb;
min-width: 3em;
padding: 0px 1px 2px 3px;
selection-background-color:#4b6eaf;
}/*:focus
{
outline: 3px ridge #4e78a2;
border-radius: 2px;
}
:edit-focus
{
border: 1px solid red;
}*/
:read-only{
background-color: transparent;
border: 1px solid #535353;
color:#999999;
}
:no-frame{
border-width: 0;
}
}
/*
* Any SpinBox (e.g. DoubleSpinbox)
*
* TODO: Icons not fit into the overall style
*/
QAbstractSpinBox::up-button{
image:url(:/qmldesigner/images/spinup.png);
}
QAbstractSpinBox::up-button:hover{
image:url(:/qmldesigner/images/spinup_hover.png);
}
QAbstractSpinBox::up-button:off{
image:url(:/qmldesigner/images/spinup_off.png);
}
QAbstractSpinBox::up-button:pressed{
image:url(:/qmldesigner/images/spinup_pressed.png);
}
QAbstractSpinBox::down-button{
image:url(:/qmldesigner/images/spindown.png);
}
QAbstractSpinBox::down-button:hover{
image:url(:/qmldesigner/images/spindown_hover.png);
}
QAbstractSpinBox::down-button:off{
image:url(:/qmldesigner/images/spindown_off.png);
}
QAbstractSpinBox::down-button:pressed{
image:url(:/qmldesigner/images/spindown_pressed.png);
}
/*
* PushButton and Utils::QtColorButton
*
* TODO: Customize :pressed
* FIXME: Outline is positioned around content, but should around padding
*/
QPushButton{
min-height: 1.33em;
min-width: 4.30em;
outline: 0;
}
Utils--QtColorButton,
QPushButton{
background-color:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #515658, stop: 1.0 #44494b); border: 1px solid #5f6161;
border-radius: 2px;
}
Utils--QtColorButton:pressed,
QPushButton:pressed{
border-style: inset;
}
QPushButton:default{
background-color:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #344a64, stop: 1.0 #263549); }
Utils--QtColorButton:disabled,
QPushButton:disabled{
background-color: transparent;
color:#999999;
}
Utils--QtColorButton:open,
QPushButton:open{
background-color:#4b6eaf;
background-color:#4b6eaf;
}
QPushButton::menu-indicator{
subcontrol-origin: content;
subcontrol-position: bottom right;
}
/*
* ComboBox
*/
QComboBox{
border-radius: 2px;
min-width: 3em;
padding: 0px 1px 2px 3px;
}
QComboBox:!editable{
/*background-color: rgba(63, 113, 73, 0.2);*/
/*background-color: #45494a;*/
background-color:rgba(255, 255, 255, 0.05);
}
/*
QComboBox:!editable:on
QComboBox::drop-down:editable:on
QComboBox::drop-down:editable
*/
QComboBox:on{
padding-top: 3px;
padding-left: 4px;
}
QComboBox::down-arrow{
image:url(:/qmldesigner/images/triangle_vert.png);
}
QComboBox::down-arrow:on{
top: 1px;
left: 1px;
}
QComboBox::drop-down{
border-left: 1px solid #646464;
}
QComboBox QAbstractItemView{
border: 1px solid red;/*black;*/
/*selection-background-color: #d2d2d2;
selection-color: #404040;
show-decoration-selected: 1; /* make the selection span the entire width of the view */ }
/*
* RadioButton
*
* TODO: Checked circle is hard to spot
*/
QRadioButton::indicator{
width: 13px;
height: 13px;
}
QRadioButton::indicator:unchecked{
image:url(:/qmldesigner/images/radiobutton_unchecked.png);
}
QRadioButton::indicator:unchecked:hover{
image:url(:/qmldesigner/images/radiobutton_unchecked_hover.png);
}
QRadioButton::indicator:unchecked:pressed{
image:url(:/qmldesigner/images/radiobutton_unchecked_pressed.png);
}
QRadioButton::indicator:checked{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论