首先建立QSS文件,内容如下:

QProgressBar{  
color : solid gray;
border: 2px solid gray;
border-radius: 5px;
background: transparent;
padding: 0px;
text-align : center ;
}
QProgressBar::chunk{
background: #B22222;
}

QTextEdit{
border: 10px;
border-image: url(image/system/border.png)10 10 10 10 ;
background-color: rgba(43,45,31,255);
}

QTextEdit[description="true"]{
border: 10px;
border-image: none;
background-color: rgba(255,255,255,255);
}

QTextEdit QScrollBar:vertical {
margin: 22px 0 22px 0;
}

QScrollBar:add-page
{
background-color: rgba(0,0,0,255);
}

QScrollBar:sub-page
{
background-color: rgba(0,0,0,255);
}

QScrollBar::add-line:vertical {

background-color: rgba(43,45,31,255);
height: 15px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}

QScrollBar::sub-line:vertical {

background-color: rgba(43,45,31,255);
height: 15px;
subcontrol-position: top;
subcontrol-origin: margin;
}

QScrollBar::handle:vertical {
border : 2px solid gray;
border-radius: 6px;
background-color: solid gray;
min-height : 12px;
}

QScrollBar::up-arrow:vertical
{
border-image: url(image/system/button/scroll-up-arrow.png);
}

QScrollBar::down-arrow:vertical
{
border-image: url(image/system/button/scroll-down-arrow.png);
}

QLineEdit#chat_edit{
background-color: rgba(20,20,20,255);
color: white;
border: 10px transparent;
height: 20px;
border-image: url(image/system/border.png)10 10 10 10 ;
}

QStatusBar::item {
border: 0px solid grey;
}

QStatusBar{
background-image: url(image/system/skill-dock.png);
color:white;
}

QMenu[private_pile="true"]{
background-color: rgba(43,63,53,200);
border-radius: 1px;
color: white;
}

QPushButton[private_pile="true"]{
background-color: rgba(43,63,53,200);
border-radius: 1px;
color: white;
}

QPushButton[game_control="true"]{
border: 0px solid gray;
font: 12px;
border-radius: 5px;
padding: 6px 12px 6px 12px;
}

QPushButton#pause{
background-image: url(image/system/button/pause.png);
}

QPushButton#play{
background-image: url(image/system/button/play.png);
}

QPushButton#slow-down{
background-image: url(image/system/button/slow-down.png);
}

QPushButton#speed-up{
background-image: url(image/system/button/speed-up.png);
}

QPushButton#uniform{
background-image: url(image/system/button/uniform.png);
}

#skill_bar_container QPushButton{
background-image: url(image/system/button/back.png);
border: 1px solid gray;
border-radius: 4px;
color: yellow;
font: 12px;
padding: 6px 12px 6px 12px;
}
#skill_bar_container QPushButton:disabled{
background-image: url(image/system/button/back_shade.png);
border-radius: 4px;
color: white;
font: 12px;
}
#skill_bar_container QCheckBox{
border: 1px solid gray;
border-radius: 4px;
color: yellow;
font: 12px;
padding: 1px 12px 1px 8px;
}
#skill_bar_container QCheckBox::indicator{
height: 0px;
width: 0px;
}
#skill_bar_container QCheckBox:unchecked{
background-image: url(image/system/button/frequent_checkbox/unchecked.png);
}
#skill_bar_container QCheckBox:unchecked:hover{
background-image: url(image/system/button/frequent_checkbox/unchecked_hover.png);
}
#skill_bar_container QCheckBox:unchecked:pressed{
background-image: url(image/system/button/frequent_checkbox/unchecked_press.png);
}
#skill_bar_container QCheckBox:checked{
background-image: url(image/system/button/frequent_checkbox/checked.png);
}
#skill_bar_container QCheckBox:checked:hover{
background-image: url(image/system/button/frequent_checkbox/checked_hover.png);
}
#skill_bar_container QCheckBox:checked:pressed{
background-image: url(image/system/button/frequent_checkbox/checked_press.png);
}
#skill_bar_container QComboBox{
background-image: url(image/system/button/back2.png);
border: 1px solid gray;
border-radius: 3px;
color: white;
padding: 1px 18px 1px 3px;
}
#skill_bar_container QComboBox:on{
padding-left: 4px;
padding-top: 3px;
}
#skill_bar_container QComboBox::drop-down{
border-bottom-right-radius: 3px;
border-left-color: darkgray;
border-left-style: solid;
border-left-width: 1px;
border-top-right-radius: 3px;
subcontrol-origin: padding;
subcontrol-position: top right;
width: 15px;
}
#skill_bar_container QComboBox::down-arrow:on{
left: 1px;
top: 1px;
}

//然后在main.cpp里面添加:

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile file("C:\\Users\\Administrator\\Documents\\widgetdemo\\css.qss");
qDebug()<<file.open(QFile::ReadOnly);

QString styleSheet = QLatin1String(file.readAll());
qDebug()<<styleSheet;
qApp->setStyleSheet(styleSheet);
MainWindow w;
w.show();

return a.exec();
}


控件可分为前景与背景

前景:多包含文件,图片等内容

背景:多包含图片,图形等内容

由于QT style是模拟CSS的布局结构,因此其满足CSS的盒子模型

从里到外的4个区域分别是:

1: content

2:  padding

3:  border

4:  margin


QSS的使用案例_#include

 

通过控制一个控件的前景,背景内容已经结构上的4个区域,我们就可以达到对一个控件为所欲为的控制

QSS基本功能

CSS的强大在于组合功能的强大,这里只是简单介绍基本功能,将简单功能组合起来才能实现好看的效果。

QSS背景属性(Background)

属性

描述

QSS

​background​

在一个声明中设置所有的背景属性。

1

​background-attachment​

设置背景图像是否固定或者随着页面的其余部分滚动。

1

​background-color​

设置元素的背景颜色。

1

​background-image​

设置元素的背景图像。

1

​background-position​

设置背景图像的开始位置。

1

​background-repeat​

设置是否及如何重复背景图像。

1

​background-clip​

规定背景的绘制区域。

3

​background-origin​

规定背景图片的定位区域。

3

​background-size​

规定背景图片的尺寸。

3

QSS边框属性(Border 和 Outline)

属性

描述

QSS

​border​

在一个声明中设置所有的边框属性。

1

​border-bottom​

在一个声明中设置所有的下边框属性。

1

​border-bottom-color​

设置下边框的颜色。

2

​border-bottom-style​

设置下边框的样式。

2

​border-bottom-width​

设置下边框的宽度。

1

​border-color​

设置四条边框的颜色。

1

​border-left​

在一个声明中设置所有的左边框属性。

1

​border-left-color​

设置左边框的颜色。

2

​border-left-style​

设置左边框的样式。

2

​border-left-width​

设置左边框的宽度。

1

​border-right​

在一个声明中设置所有的右边框属性。

1

​border-right-color​

设置右边框的颜色。

2

​border-right-style​

设置右边框的样式。

2

​border-right-width​

设置右边框的宽度。

1

​border-style​

设置四条边框的样式。

1

​border-top​

在一个声明中设置所有的上边框属性。

1

​border-top-color​

设置上边框的颜色。

2

​border-top-style​

设置上边框的样式。

2

​border-top-width​

设置上边框的宽度。

1

​border-width​

设置四条边框的宽度。

1

​outline​

在一个声明中设置所有的轮廓属性。

2

​outline-color​

设置轮廓的颜色。

2

​outline-style​

设置轮廓的样式。

2

​outline-width​

设置轮廓的宽度。

2

​border-bottom-left-radius​

定义边框左下角的形状。

3

​border-bottom-right-radius​

定义边框右下角的形状。

3

​border-image​

简写属性,设置所有 border-image-* 属性。

3

​border-image-outset​

规定边框图像区域超出边框的量。

3

​border-image-repeat​

图像边框是否应平铺(repeated)、铺满(rounded)或拉伸(stretched)。

3

​border-image-slice​

规定图像边框的向内偏移。

3

​border-image-source​

规定用作边框的图片。

3

​border-image-width​

规定图片边框的宽度。

3

​border-radius​

简写属性,设置所有四个 border-*-radius 属性。

3

​border-top-left-radius​

定义边框左上角的形状。

3

​border-top-right-radius​

定义边框右下角的形状。

3

box-decoration-break

3

​box-shadow​

向方框添加一个或多个阴影。

3

Box 属性

属性

描述

QSS

​overflow-x​

如果内容溢出了元素内容区域,是否对内容的左/右边缘进行裁剪。

3

​overflow-y​

如果内容溢出了元素内容区域,是否对内容的上/下边缘进行裁剪。

3

​overflow-style​

规定溢出元素的首选滚动方法。

3

​rotation​

围绕由 rotation-point 属性定义的点对元素进行旋转。

3

​rotation-point​

定义距离上左边框边缘的偏移点。

3

QSS 字体属性(Font)

属性

描述

QSS

​font​

在一个声明中设置所有字体属性。

1

​font-family​

规定文本的字体系列。

1

​font-size​

规定文本的字体尺寸。

1

​font-size-adjust​

为元素规定 aspect 值。

2

​font-stretch​

收缩或拉伸当前的字体系列。

2

​font-style​

规定文本的字体样式。

1

​font-variant​

规定是否以小型大写字母的字体显示文本。

1

​font-weight​

规定字体的粗细。

1

QSS 外边距属性(Margin)

属性

描述

QSS

​margin​

在一个声明中设置所有外边距属性。

1

​margin-bottom​

设置元素的下外边距。

1

​margin-left​

设置元素的左外边距。

1

​margin-right​

设置元素的右外边距。

1

​margin-top​

设置元素的上外边距。

1

QSS 内边距属性(Padding)

属性

描述

QSS

​padding​

在一个声明中设置所有内边距属性。

1

​padding-bottom​

设置元素的下内边距。

1

​padding-left​

设置元素的左内边距。

1

​padding-right​

设置元素的右内边距。

1

​padding-top​

设置元素的上内边距。

1

QSS 定位属性(Positioning)

属性

描述

QSS

​bottom​

设置定位元素下外边距边界与其包含块下边界之间的偏移。

2

​clear​

规定元素的哪一侧不允许其他浮动元素。

1

​clip​

剪裁绝对定位元素。

2

​cursor​

规定要显示的光标的类型(形状)。

2

​display​

规定元素应该生成的框的类型。

1

​float​

规定框是否应该浮动。

1

​left​

设置定位元素左外边距边界与其包含块左边界之间的偏移。

2

​overflow​

规定当内容溢出元素框时发生的事情。

2

​position​

规定元素的定位类型。

2

​right​

设置定位元素右外边距边界与其包含块右边界之间的偏移。

2

​top​

设置定位元素的上外边距边界与其包含块上边界之间的偏移。

2

​vertical-align​

设置元素的垂直对齐方式。

1

​visibility​

规定元素是否可见。

2

​z-index​

设置元素的堆叠顺序。

 

 

 

QSS 文本属性(Text)

属性

描述

QSS

​color​

设置文本的颜色。

1

​direction​

规定文本的方向 / 书写方向。

2

​letter-spacing​

设置字符间距。

1

​line-height​

设置行高。

1

​text-align​

规定文本的水平对齐方式。

1

​text-decoration​

规定添加到文本的装饰效果。

1

​text-indent​

规定文本块首行的缩进。

1

text-shadow

规定添加到文本的阴影效果。

2

​text-transform​

控制文本的大小写。

1

​unicode-bidi​

设置文本方向。

2

​white-space​

规定如何处理元素中的空白。

1

​word-spacing​

设置单词间距。

1

​hanging-punctuation​

规定标点字符是否位于线框之外。

3

​punctuation-trim​

规定是否对标点字符进行修剪。

3

text-align-last

设置如何对齐最后一行或紧挨着强制换行符之前的行。

3

​text-emphasis​

向元素的文本应用重点标记以及重点标记的前景色。

3

​text-justify​

规定当 text-align 设置为 "justify" 时所使用的对齐方法。

3

​text-outline​

规定文本的轮廓。

3

​text-overflow​

规定当文本溢出包含元素时发生的事情。

3

​text-shadow​

向文本添加阴影。

3

​text-wrap​

规定文本的换行规则。

3

​word-break​

规定非中日韩文本的换行规则。

3

​word-wrap​

允许对长的不可分割的单词进行分割并换行到下一行。

3