【问题标题】:QT pushbutton with image how to transparent/opacity带有图像的 QT 按钮如何透明/不透明
【发布时间】:2021-09-18 15:55:36
【问题描述】:

我有一个按钮,并在 QSS 中设置了样式以分配这样的图像

QPushButton[type="buttonImgType"] {
    image: url(:images/svg/myIcon.svg);
    image-position: center;
    min-height: 42px;
    min-width: 130px;
}

我希望此按钮在未选中时显示为褪色或 50% 透明 并显示完整图像。但我找不到如何将 QT 中的属性用于按钮的方法。

有人知道怎么做吗?

【问题讨论】:

    标签: qt qtgui


    【解决方案1】:

    关注@Nejat @​​987654321@:

    您可以通过设置 QLabel 或 QPushbutton 来设置透明度 样式表:

    ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    ui->button->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    

    您还可以在设计器中将背景颜色:rgba(255, 255, 255, 0); 添加到小部件的 styleSheet 属性中。

    第四个参数是alpha。你也可以有半透明 通过将 alpha 设置为大于零的某个值来创建小部件:

    ui->button->setStyleSheet("background-color: rgba(255, 255, 255, 50);");
    

    可能重复 C++ over Qt : Controlling transparency of Labels and Buttons

    【讨论】:

      【解决方案2】:

      试试这种风格:

      QPushButton
      {
         background-color: transparent;
         border: none;
      
      }
      QPushButton:pressed
      {
         background-color:rgba(239, 41, 41,50);
         border:2px solid black;
      }
      

      直到 QPushButton 有边框它才透明。

      【讨论】:

        猜你喜欢
        • 2015-11-19
        • 2019-12-13
        • 2020-06-11
        • 2019-09-21
        • 2011-09-15
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        相关资源
        最近更新 更多