QT样式表 ——styleSheet (样式规则+用法)

一 代码设置

1.QApplication::setStyleSheet() 在应用程序上,一般在main()函数中:

QFile file(":/qss/abc.qss"); //工程控件样式表需要在qss/abc.qss里面

file.open(QFile::ReadOnly);

styleSheet = QLatin1String(file.readAll());

qApp->setStyleSheet(styleSheet);

file.close();

2.QWidget::setStyleSheet()在小组件上,

QPushButton *btn = new QPushButton();

Btn-> setStyleSheet(“color : red”);//表示字体颜色为红色

3.在QT Designer 中设置

单个控件按钮——要修改样式的控件右击->改变样式表

所有的控件 ——在顶级窗口右击控件->改变样式表

二 UI界面设置

<1>规则1:代码构成:选择器+声明     如下:

QPushButton { color : red }

  PS1:多个选择器   逗号(,)分隔。

QPushButton, QLineEdit, QComboBox {color: red}

  PS2:多个属性。用分号( ; )分割

QPushButton { color: red; background-color: white }

<2>规则2:不同选择器的格式(详细文章:https://www.cnblogs.com/that-boy-done/p/11458130.html

1 * { 属性: 值; }   //通用选择器  匹配程序中所有的 widget如 *{font: normal 20px “微软雅黑”;
 
2 类名 { 属性: 值; }//类型选择器  如 QPushButton{ color: blue; }

3 .类名 { 属性: 值; }//类选择器 匹配类的所有对象, 不匹配派生类.如.QPushButton{ color: blue; }

4 #id{ 属性: 值; }//ID 选择器 指定的名称的对象 如 #button_1{ color: red; }

5 选择器 1 选择器 2{ 属性: 值; }//后代选择器 可用空格一直延续  一般只要B在A上,就是后代 
    不仅可以使用类型选择器, 还可以使用类选择器, id 选择器等.
    Qt中, 实际父子关系取决于如何布局.如 BaseDialog QPushButton{min-width: 120px;}
6 选择器 1 >选择器 2 { 属性: 值; }//子元素选择器 不能延续只能有一个”>”
    只会查找”儿子”, 不会查找其他后代
    不仅可以使用类型选择器, 还可以使用类选择器,id 等选择器
    Qt特别注意继承关系
    子元素选择器一般用于一些特定布局条件中的控件, 如直接布局在QGroupBox的QCheckBox设置特定属性
    .QGroupBox > .QCheckBox{color: blue;}      
7 属性选择器

8并集选择器

9伪类选择器等

发布了101 篇原创文章 · 获赞 3 · 访问量 6321

猜你喜欢

转载自blog.csdn.net/qq_37631516/article/details/104911449