【QT】QT从零入门教程(十六):QSS样式表

x33g5p2x  于2022-03-06 转载在 其他  
字(14.8k)|赞(0)|评价(0)|浏览(935)

实现一个软件的基本界面和功能后,接下来要做的就是美化界面,这就用到了QSS样式表。

QSS包含了一个样式规则序列,一个样式规则由一个选择器和声明组成,选择器指定哪些部件由规则影响,声明指定哪些属性应该在部件上进行设置。例如:
QPushButton { color: red }

其中QPushButton是选择器,{ color: red }是声明,该规则指定QPushButton及其子类(例如:MyPushButton)应使用红色作为前景色。QSS通常不区分大小写(即:color、Color、COLOR、cOloR指同一属性),唯一例外就是类名(class names)、对象名(object names)、属性名(property 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的qss-dracula)

/*
 * 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;
    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;
    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;
}
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 {
    image: url(:/qmldesigner/images/radiobutton_checked.png);
}
QRadioButton::indicator:checked:hover {
    image: url(:/qmldesigner/images/radiobutton_checked_hover.png);
}
QRadioButton::indicator:checked:pressed {
    image: url(:/qmldesigner/images/radiobutton_checked_pressed.png);
}

/*
 * TabWidget and TabBar
 *
 * FIXME: Designer bottom tabbar problem
 * TODO: Customize QTabBar::tab:hover
 */
QTabWidget::tab-bar {
    /*position: relative;
    bottom: -1px;*/
}
QTabWidget::pane {
    background-color: transparent;
    border: 1px solid #323232;
}
QTabWidget::pane:top {
    top: -1px;
}
QTabWidget::pane:right {
    right: -1px;
}
QTabWidget::pane:bottom {
    bottom: -1px;
}
QTabWidget::pane:left {
    left: -1px;
}
QTabBar::tab {
    border: 1px solid #292b2d;
    min-width: 8ex;
    padding: 4px;
}
/*QTabBar::tab:hover {
    background-color: #4b6eaf;
}*/
QTabBar::tab:top {
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
}
QTabBar::tab:right {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
QTabBar::tab:bottom {
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
}
QTabBar::tab:left {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}
QTabBar::tab:selected {
    background-color: #41516d;
}
QTabBar::tab:top:selected {
    border-bottom-color: #41516d;
}
QTabBar::tab:right:selected {
    border-left-color: #41516d;
}
QTabBar::tab:bottom:selected {
    border-top-color: #41516d;
}
QTabBar::tab:left:selected {
    border-right-color: #41516d;
}
QTabBar::tab:top:!selected {
    margin-top: 2px;
}
QTabBar::tab:right:!selected {
    margin-right: 2px;
}
QTabBar::tab:bottom:!selected {
    margin-bottom: 2px;
}
QTabBar::tab:left:!selected {
    margin-left: 2px;
}

/*
 * Table
 *
 * FIXME: Problem with arrows positioning workarounded in bad way
 * FIXME: Unknown problem Options > FakeVim > User Command Mapping
 * QT-BUG: Wrong behaviour property min-height of QHeaderView::section
 */

QTableWidget,
QTableView {
    /* This rule have more priority than previous?!
     * And overrides with empty if not declared! */
    /*background: red; /*transparent;*/
    /*color: #bbbbbb;*/

    border: 1px solid #323232;
    alternate-background-color: #414547; /* Dont work without setAlternatingRowColors(true) */
}
QTableView QTableCornerButton::section {
    background-color: green; /*FIXME*/
    border: 2px outset blue; /*FIXME*/
}
QHeaderView::section {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #54585b, stop:1 #393c3e);
    color: #bbbbbb;
    padding: 1px 2px 1px 4px;

    border: 1px solid #323232;
    border-top-width: 0;
    border-left-color: #5e6163;
    border-right-color: #2a2c2d;

    /*min-height: 15px;*/
}
QHeaderView::section:hover {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #64686b, stop:1 #494c4e);
    border-bottom-color: #424242;
}
QHeaderView::section:first {
    border-left-width: 0;
}
QHeaderView::section:last {
    border-right-width: 0;
}
QHeaderView::section:checked {
    background-color: red; /*FIXME*/
}
QHeaderView::down-arrow {
    image: url(:/qmldesigner/images/down_arrow.png);
    right: 7px;
}
QHeaderView::up-arrow {
    image: url(:/qmldesigner/images/up_arrow.png);
    /* image-position: right; Don't work */
    /*right: 7px; */
    height: 7px;
    width: 7px;
}

/*
 * Menu and MenuBar
 *
 * TODO: Customize toggleable items
 */
QMenuBar {
    /*background-color: #3c3f41; /* transparent don't work */
    border-bottom: 2px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #282828, stop:1 #555555);
}
QMenuBar::item:pressed {
    background-color: #4b6eaf;
}
QMenuBar::item:selected:!pressed {
    background-color: #585b5d;
}
QMenu {
    border: 1px solid #2d2d2d;
}
/*QMenu::item {
  margin-left: 3px;
}*/

QMenu::item:disabled {
    color: #999999;
}
QMenu::item:selected {
    background-color: #4b6eaf;
}
QMenu::icon {
    border: 0px solid transparent;
    background-color: transparent;
}
QMenu::icon:checked {
    background-color: blue;
    border: 1px inset red;
    position: absolute;
    top: 1px;
    right: 1px;
    bottom: 1px;
    left: 1px;
}
QMenu::separator {
    height: 2px;
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #282a2b, stop:1 #45484b);
    margin: 0 1px;
}
/*QMenu::indicator {
    width: 13px;
    height: 13px;
    background-color: blue;
}*/

/*
 * Scroll bar modifications
 */
QScrollBar {
    background-color: transparent;
    margin: 0;
    height: 12px; /* for horizontal */
    width: 12px; /* for vertical */
}
QScrollBar::handle {
    border: 1px solid #555555;
    border-radius: 4px;
    margin: 1px;
}
QScrollBar::handle:vertical {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop:0 #5c5c5c, stop:1 #515151);
    min-height: 20px;
    min-width: 8px;
}
QScrollBar::handle:vertical:hover {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop:0 #666666, stop:1 #5b5b5b);
}
QScrollBar::handle:horizontal {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #5c5c5c, stop:1 #515151);
    min-height: 8px;
    min-width: 20px;
}
QScrollBar::handle:horizontal:hover {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #666666, stop:1 #5b5b5b);
}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical,
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {
    background-color: transparent;
}
/*QScrollBar::sub-page:vertical {
    max-height: 8px;
    margin-top: auto;
    margin-bottom: 0;
    background-color: purple;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
}
QScrollBar::add-page:vertical {
    max-height: 8px;
    margin-top: 0;
    margin-bottom: auto;
    background-color: darkblue;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
}*/
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical,
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {
    height: 0;
    width: 0;
}

/*
 * Status bar
 */
/*
QStatusBar {
    background-color: brown;
    border: 1px solid brown;
}
QStatusBar::item {
    background-color: red;
    border: 1px solid red;
    border-radius: 3px;
}
QStatusBar QLabel {
    background-color: green;
    border: 3px solid green;
}
*/

/*
 * QAbstractItemView modifications
 *
 * TODO: ::indicator
 * TODO: Disable item icon selection mask
 * FIXME: Transparent selection background on some elements Options > BlackBerry > API Levels and Runtimes
 * QT-BUG: Strange behaviour of selection-* properties (always :!focus)
 */
QAbstractItemView {
    border: 1px solid #323232;
    selection-color: #bbbbbb;
    selection-background-color: #4b6eaf;
    alternate-background-color: #414547; /* Works only if setAlternatingRowColors(true) */
    show-decoration-selected: 1;
    outline: 0;
}
/*
QAbstractItemView:!focus {
    selection-background-color: #0d293e;
}
QTreeView::branch,
QAbstractItemView::item {
    border-width: 0;
}
*/
QAbstractItemView::item:alternate {
    background-color: #414547;
}
QListWidgetItem,
QTreeWidgetItem  {
    background-color: red;
    color: yellow;
    border: 1px solid purple;
}
QAbstractItemView::text {
    color: #bbbbbb;
}
QTreeView::branch:hover,
QAbstractItemView::item:hover {
    background-color: rgba(255, 255, 255, 0.05);
}
QTreeView::branch:selected:active,
QAbstractItemView::item:selected {
    background-color: #4b6eaf;
}
/* No way due to another bug :(
QTreeView::branch:selected:active,
QAbstractItemView::item:selected:active {
    background-color: #4b6eaf;
}
QTreeView::branch:selected:!active,
QAbstractItemView::item:selected:!active {
    background-color: #0d293e;
}
*/
QTreeView::branch:has-siblings:adjoins-item,
QTreeView::branch:has-siblings:!adjoins-item,
QTreeView::branch:!has-children:!has-siblings:adjoins-item {
    border-image: none;
    image: none;
}
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings {
    border-image: none;
    image: url(:/qmldesigner/images/triangle_horz.png);
}
QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings {
    border-image: none;
    image: url(:/qmldesigner/images/triangle_vert.png);
}

/*
 * Tool bar
 */

QToolBar {
    border-width: 0; /* Cancels gradiend */
}

/*
 * Resizing widget (bottom right in Qt Creator, inside status bar)
 */

QSizeGrip {
    image: url(:/qmldesigner/images/sizegrip.png);
    width: 16px;
    height: 16px;
}

/*
 * ToolTip
 */

QToolTip, QTipLabel {
    border: 1px solid #9a9a66;
    border-radius: 3px;
    background: #5c5c42;
    color: #bbbbbb;
    margin: 0;
    padding: 0;
}

/*
 * Wizard dialog
 *
 * TODO: Buttons bar background
 */

QWizard > QWidget {
    background-color: #3c3f41;
}

/* QT-BUG: Because QFrame { border-color: #323232; } has no effect */
Core--Internal--NewDialog > QFrame#frame {
    border: 1px solid #323232;
}

/* vvvvvvvvvvvvvvvvvvvvvvvvvv WORKAROUND vvvvvvvvvvvvvvvvvvvvvvvvvv */
/* Related to:
 *   1) https://bugreports.qt-project.org/browse/QTCREATORBUG-12078
 *   2) https://codereview.qt-project.org/#/c/74538/
 * Because of bugs in QtGui, apllying stylesheet to QPlainTextEdit causes it
 * to lose font settings and palette on setParent event. So switching between
 * modes (Editor and Debug) or split Editor resets font and color of edit
 * and extra edit areas
 * To workaround this problems set you own peferred colors and font below
 */
QPlainTextEdit {
    background-color: #2b2b2b;
    selection-background-color: #214283;
    color: #bbbbbb;
    selection-color: #bbbbbb; /* No better workaround for this */
    font: 10pt Courier, Inconsolata, Monospace;
}
/*TextEditor--Internal--TextEditExtraArea*/
TextEditor--TextEditorWidget > QWidget {
    background-color: #313335;
    selection-background-color: #4b6eaf;
    color: #bbbbbb; /* QT-BUG: Sadly this has no effect */
}
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^ WORKAROUND ^^^^^^^^^^^^^^^^^^^^^^^^^^ */

相关文章