QPushButton没有正确更改背景颜色 [英] QPushButton is not changing the background-color proper

查看:1119
本文介绍了QPushButton没有正确更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对QPushButton有问题. 我使用QT 4.8和Designer.

I have a problem with a QPushButton. I use QT 4.8 and the Designer.

我将按钮配置为扁平"和可检查". 如果未选中,则按钮应为红色;如果选中,则按钮应为绿色. 为了给按钮设置样式,我使用了样式表.

I configured the button to be "flat" and "checkable". The button should be red if not checked and green if checked. To style the button I use a stylesheet.

      QPushButton:default{
         background-color: red;
             color: red;
      }

      QPushButton:checked{
         background-color: green;
             color: black;
      }

现在是问题所在.如果未选中该按钮,则为灰色.当我按下按钮时,他会像应该的那样变成绿色. 我尝试了其他伪状态,例如!checked,或试图更改为普通按钮.但是总是一样的.在默认状态下,样式表不起作用.如果我按,悬停或按钮按我想要的方式改变.

Now the problem. If the button is unchecked it is gray. When I press the button he turns green like it should. I tried different other pseudo-states, like !checked, or tried to change to a normal button. But it's always the same. In default state the stylesheet isn't working. If I presse, hover or what ever the button it is changing like I want it.

有没有人遇到同样的问题并有解决方案?

Any body encountered the same problem and has a solution?

进一步解释一下. color属性正常工作. 文本始终为红色,但我按下按钮后为黑色. 因此正在使用样式表,但是只有background-color属性无法正常工作.

To explain it a little bit further. The color attribute is working correct. The text is always red, except I push the button then it's black. So the style sheet is in use but, just the background-color attribute is not working.

我尝试了不同的样式,例如主题,cde,cleanlooks等,并且总是一样.

I tried different styles, like motif, cde, cleanlooks etc and it's always the same.

推荐答案

我在QPushButton类参考中找到了这个. 此属性的默认值为false.如果设置了此属性,除非按下按钮,否则大多数样式都不会绘制按钮背景.setAutoFillBackground()可用于确保使用QPalette :: Button画笔填充背景."因此,我找到了以下解决方案.

I found this in QPushButton Class Reference. "This property's default is false. If this property is set, most styles will not paint the button background unless the button is being pressed. setAutoFillBackground() can be used to ensure that the background is filled using the QPalette::Button brush." Because of this I found the following solution.

首先,我将样式表选项保留为选中状态

First of all I kept the style sheet option for checked

     QPushButton:checked{
         background-color: green.
     }

我使用了调色板,并设置了背景色和setAutoFillBackground函数.

The I used a palette and set the background color and the setAutoFillBackground function.

     palette_red->setColor(window.button->backgroundRole(), QColor(255, 0, 0, 127));
     window.button->setAutoFillBackground(true);
 window.button->setPalette(*palette_red);

我使用切换信号来捕捉变化. 当按钮切换为TRUE时,样式表开始工作. 为了不使颜色变双(绿色对红色),必须再次关闭setAutoFillBackground.

I use the toggle signal to catch changes. When the button is toggle is TRUE the style sheet starts working. To not get double (green over red) colors the setAutoFillBackground has to be turned off again.

     window.button->setAutoFillBackground(false);

未切换状态几乎相同. 我需要打开setAutoFillBackground并再次设置调色板.

The not toggled state is nearly the same. I needed to turn on the setAutoFillBackground and set the palette again.

     window.button->setAutoFillBackground(true);
     window.button->setPalette(*palette_red);

这是一个解决方案,它可以工作,但我仍然愿意接受进一步的输入.

It's a solution, it works but I am still open for further inputs.

这篇关于QPushButton没有正确更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆