得到QPushButton按下背景颜色

时间:2016-04-08 05:51:06

标签: c++ qt qcolor qpalette

如何获得QPushButton的按下背景颜色?

if (isDown())
    _BGColor = <pressed background color>; // how to get this???
else
    _BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor

提前致谢!!!

1 个答案:

答案 0 :(得分:1)

在按下按钮时找到按钮的背景颜色非常困难(如果不是不可能),因为它取决于样式,并且不能保证样式尊重调色板。

但我建议采用两种不同的方法:

  1. 您可以使用样式表(更简单)设置​​自己的背景颜色,或者自己使用样式或重新实现paintEvent()来实现按钮的绘制。请参阅Customizing QPushButton

  2. 要使用反色绘制按钮,可以为画家设置合成模式以获得反色。

  3. 例如:

    painter.setPen(QColor(255, 255, 255));
    painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
    

    (请注意,使用此示例,中间灰色(128,128,128)的反色颜色完全相同)

    请参阅QPainter::CompositionMode

相关问题