QPushButton样式表设置:setStyleSheet()

本文使用Qt版本为Qt 4.8。


代码示例:

QPushButton pushButton("Beauty");;

pushButton.setStyleSheet(
            "QPushButton{"                             // 正常状态样式
            "background-color: rgb(50, 50, 50);"       // 背景色(也可以设置图片)
            "color: white;"                            // 字体颜色
            "font: bold 13px;"                         // 字体: 加粗 大小
            "border-radius: 5px;"                      // 边框圆角半径像素
            "border: 2px solid rgb(50, 50, 50);"       // 边框样式:border-width border-style border-color
            "border-style:outset;"                     // 定义一个3D突出边框,inset与之相反
            "text-align: left;"                        // 文本:左对齐
            "}"

            "QPushButton:focus{"                       // 聚焦样式
            "background-color:rgb(255, 128, 64);"
            "color: white;" 
            "border-radius: 5px;"
            "border: 2px solid white;"
            "border-style:outset;" 
            "font:bold 13px;"
            "text-align: left;"
            "}");

1、"QPushButton{"、 "QPushButton:focus{" 指定了不同情况下显示其对应的样式。假设该按钮setFocus之后,Qt就会根据上面的第二段的样式设置进行显示。

2、其它常用情况还包括

QPushButton:pressed 按钮被按下
QPushButton:hover 鼠标停在按钮的位置

想了解更多可以查看官网的帮助文档:List of Pseudo-States | Qt 4.8。

3、另一种写法,如:

QString styleSheet = "QPushButton{background-color: rgb(50, 50, 50); color: white;}";
pushButton.setStyleSheet(styleSheet);

4、关于样式的语法规则,可通过官方文档简单了解:The Style Sheet Syntax | Qt 4.8。

此外,农夫在学习CSS样式时,更多是在 CSS 教程 | 菜鸟教程 通过其搜索功能学习的,在此推荐,共勉。

QPushButton样式表设置:setStyleSheet()_第1张图片

以上是农夫今天刚接触StyleSheet所了解的内容,便简单总结一下。如有不足之处,请多多指教。


参考:

setStyleSheet来设定窗口部件的样式_网络_Seanyxie Blog -CSDN博客
Qt--样式表设置按钮样式_ui_sinan1995的博客-CSDN博客

你可能感兴趣的:(Qt,qt4,stylesheet,css)