Qt第十二章 样式表

样式表

1.样式表

盒子模型

在这里插入图片描述

2.选择器

  • 样式表语法,选择器{属性1:值;属性2:值;}
  • 如果只有单个属性,分号可以不加

选择器类型

选择器 示例 描述
通用选择器 * 匹配所有控件
类型选择器 QPushButton 匹配给定的控件,包括子类
类选择器 .QPushButton 匹配给定的控件,不包括子类
属性选择器 QPushButton[flat=“false”] 匹配给定类型中符合[属性]的控件
ID选择器 QPushButton#closeButton 匹配给定类型,且对象名为closeButton的控件
子孙对象选择器 QDialog QPushButton 匹配给定类型的子孙控件
子对象选择器 QDialog>QPushButton 匹配给定类型的直接子控件
辅助[子控件]选择器 QComboxBox::drop-down 复杂对象子控件
伪状态选择器 QPushButton:hover 控件的特定状态下的样式
并集选择器 QPushButton,QLineEdit 若干基础选择器可以写一起,逗号隔开
QPushButton[down = "false"]{color:yellow;background:red}
QLabel[text = "TextLabel"]{color:red}

同时在UI和文件代码里设置样式表会冲突,只会生效ui里设置的

伪状态选择器Pseudo-State

QPushButton:hover{color:red}
状态 描述
:disabled 控件禁用
:enabled 控件启用
:focus 控件获取输入焦点
:hover 鼠标在控件上悬停
:pressed 鼠标按下
:checked 控件被选中
:unchecked ~
:indetetminate 控件部分被选中
:open 控件打开
:closed ~
:on 控件可以切换,且处于on状态
:off ~
! 对以上状态的否定

3.控件示例

QLabel#label{
 font-family: "微软雅黑";
font-size: 20px;
 font-style: italic;
color: rgb(239,204,180);
border-image:url("C:/Users/PVer/Pictures/Resource/派蒙.jpeg")
}

QPushButton{
/*上右下左设置padding间距*/
padding:20px 0px 0px 50px;
border: 3px solid black;
border-radius: 10px;
background-color:red;
}

QLineEdit{
border-top:2px solid yellow;
border-right:2px dotted black;
border-top-left-radius:10px;
border-left:3px solid black;
}

QLabel#label_2{
background-image:url("C:/Users/PVer/Pictures/Resource/xiaoku.png");
background-repeat:no-repeat;
background-position:left top;
}

QLabel#label_2:hover{
background-color:green;
}

QPushButton:pressed{
background-color:orange;
}

QCheckBox::indicator{
background-color:grey;
border-radous:5px;
}

QCheckBox::indicator:checked{
background-color:black;
}

QCheckBox::indicator:!checked:hover{
background-color:green
}

在这里插入图片描述

4

.细节、注意事项

继承自QWidget的类,设置qss样式表没有效果,需要重写paintEvent

 void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.initFrom(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }

而且必须使用Q_OBJECT宏

相关推荐

  1. git

    2024-07-17 16:44:04       48 阅读
  2. 认识Ajax()

    2024-07-17 16:44:04       43 阅读
  3. 信息安全

    2024-07-17 16:44:04       30 阅读
  4. Linux

    2024-07-17 16:44:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 16:44:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 16:44:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 16:44:04       58 阅读
  4. Python语言-面向对象

    2024-07-17 16:44:04       69 阅读

热门阅读

  1. linux 修改hostname

    2024-07-17 16:44:04       23 阅读
  2. 【Oracle】Oracle语法之递归查询

    2024-07-17 16:44:04       19 阅读
  3. C++基础练习 - Chapter 3

    2024-07-17 16:44:04       17 阅读
  4. 如何成为一个厉害的人

    2024-07-17 16:44:04       22 阅读
  5. Web开发-LinuxGit基础6-本地-.gitignore

    2024-07-17 16:44:04       18 阅读
  6. 运动控制:步进电机同步带传动距离计算

    2024-07-17 16:44:04       18 阅读
  7. Spring与设计模式总览

    2024-07-17 16:44:04       19 阅读
  8. Avalonia中的数据验证

    2024-07-17 16:44:04       21 阅读
  9. [ptrade交易实战] 第十五篇 融资融券交易类函数

    2024-07-17 16:44:04       24 阅读