zoukankan      html  css  js  c++  java
  • Qt中使用setStyleSheet对QPushButton按钮进行外观设置

    Qt中使用setStyleSheet对按钮进行外观设置

    字体颜色的设置一般时以下两种方案:

    (1)属于QWidget子类的一些控件

    可以直接使用样式表,例如label->setStyleSheet("color:white");
    (2)不属于QWidget子类的控件

    可以考虑设置其前景色,例如各种ViewtreeWidgetItem->setForeground(0,QBrush(QColor(Qt::white)));

    -------------------------------------------------------------------------------------------------

    要实现的效果

    正常状态下:黑底(背景色),白字(前景色),圆角,向外凸起;

    鼠标停留:背景和前景都反色;

    鼠标按下:背景色变为淡蓝色,向内凹陷。

    代码:

    ui->pushButton_GoToProcess->setStyleSheet("QPushButton{background-color:black;

                                                color: white;   border-radius: 10px;  border: 2px groove gray;

                                                border-style: outset;}"

                                               "QPushButton:hover{background-color:white; color: black;}"

                                              "QPushButton:pressed{background-color:rgb(85, 170, 255);

                                                               border-style: inset; }"

                                               );

    结果:

    (1)正常状态

    (2)鼠标停留


    (3)鼠标按下


    -------------------------------------------------------------------------------------------------

    如果要对多个按钮实现同样的效果,只要将setStyleSheet里的字符串定义成QString,后面其他按钮直接调用这个QString就好了。

    定义:

    QString button_style="QPushButton{background-color:black;

                                          color: white;   border-radius: 10px;  border: 2px groove gray;

                                          border-style: outset;}"

                                         "QPushButton:hover{background-color:white; color: black;}"

                                        "QPushButton:pressed{background-color:rgb(85, 170, 255);

                                                         border-style: inset; }";

    调用:

    ui->pushButton_Save->setStyleSheet(button_style);



    -------------------------------------------------------------------------------------------------

    对于按钮如果想设置为图片,则最好使用添加icon,这样的话位置比较合适,如果是使用setStyleSheet还需要对位置进行设置(直接设置会发现位置有点偏)。另外要加上一句:

  • 相关阅读:
    IOS 字典转模型
    iOS UIView 快速修改 frame
    IOS 解析JSON
    IOS 解析XML--使用NSXML
    ios 解析json,xml
    StringBuffer类和String类的区别
    Thread类和Runnable接口的比较
    Exception和RuntimeException的区别
    代理设计模式
    工厂设计模式
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648119.html
Copyright © 2011-2022 走看看