toryano0820
toryano0820

Reputation: 41

Get QPushButton Pressed Background Color

How can I get the pressed background color of a QPushButton?

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

Thanks in advance!!!

Upvotes: 1

Views: 589

Answers (1)

Fabio
Fabio

Reputation: 2602

It's very difficult (if not impossible) to find a way to get the backgound color of a button when it's pressed, because it depends on the style, and it's not guaranteed that the style respects the palette.

However I suggest two different approaches:

  1. You can set you own background color using style sheets (simpler) or implement the painting of the button yourself, using styles or reimplementing paintEvent(). See Customizing QPushButton

  2. To paint over the button with the inverse color, you can set a composition mode to the painter in order to get the inverse color.

For example:

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

(note that using this example, the inverse color of middle grey (128, 128, 128) is exactly the same color)

See QPainter::CompositionMode

Upvotes: 1

Related Questions