您的位置:首页 > 汽车 > 新车 > Qt 样式表、选择器、盒子模型

Qt 样式表、选择器、盒子模型

2024/10/7 8:53:45 来源:https://blog.csdn.net/qq_42432673/article/details/141598194  浏览:    关键词:Qt 样式表、选择器、盒子模型

1、两种样式表的写法
方式一(普通写法):

    this->setStyleSheet("QPushButton""{""background:yellow;""font:bold 14px;""color: red;""}""QPushButton:hover""{""background-color:gray;""color:white;""}""QLabel""{""background-color:blue;""}");

方式二(使用R字符串优化):

    this->setStyleSheet(R"(QPushButton{background:yellow;font:bold 14px;color: red;}QPushButton:hover{background-color:gray;color:white;}QLabel{background-color:blue;})");

值得注意的是setStyleSheet()该方法只能用于设置有父窗口的子窗口的样式,如果一个窗口没有父窗口,则使用setStyleSheet()不起作用。

2、常用的选择器类型
请添加图片描述
3、给不同的选择器指定相同的属性和值

QPushButton, QLineEdit, QComboBox { color: red }

4、子控件示例

QComboBox::drop-down { image: url(dropdown.png) }

5、伪状态示例

QPushButton:hover { color: white }
QRadioButton:!hover { color: red }/*使用感叹号表示否定,鼠标没有悬停到QRadioButton时才应用规则*/
QCheckBox:hover:checked { color: white }/*伪状态多个连用,达到逻辑与效果*/
QCheckBox:hover, QCheckBox:checked { color: white }/*这样写可以达到逻辑或效果*/
QComboBox::drop-down:hover { image: url(dropdown_bright.png) }/*伪状态和子控件联合使用*/

6、盒子模型
当使用样式表时,每一个部件都被看作拥有4个同心矩形的盒子模型,如下图所示:
在这里插入图片描述
边距(margin)、边框宽度(border)和填衬(padding)等属性的默认值是0,这样四个矩形恰好重合。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com