QPushButton Overlay Color
Well I basically was to make a QPushButton which has a color contained with in it, basically a box with a color overlapped on top of a button. When you press the button it'll open a QColorDialog which you than pick a color, setting the color overlapped on top of the button to the newly selected color. I'd prefer to not make a new control so I was wondering if there is a way to do this.
The easiest way to do it is to reimplement paintEvent().
Something like (not tested)
@void ColorButton::paintEvent( QPaintEvent* event )
QPainter painter( this );
painter.setBrush( QBrush( currentColor_ ) );
painter.drawRect( event->rect() );
In your reimplementation, you forgot to include a call to the base class implementation. The code above will only draw the square.
void ColorButton::paintEvent( QPaintEvent* event )
QPushButton::paintEvent(event); // <-- do not forget this line
QPainter painter( this ); painter.setBrush( QBrush( currentColor_ ) ); painter.drawRect( event->rect() );
You might also considder just creating a pixmap, and setting that on the button with setPixmap if the color changes. You can do that either in a subclass or separately from the button code.
Andre, that was intentionally. Both of yours and mine functions will draw only a rectangle (painter.drawRect( event->rect() ) completely paints out base button) so there is no need to call base class implementation.
Doh, you are right of course.
Then again, I would not draw the rectangle over the whole button, normally. But indeed, my version too will yield the same result at lower performance than yours.