I have a custom QWidget
(actually, derived from QAbstractButton
) for which I have to implement my own paintEvent
. How to I use the style sheet information?
For example, suppose someone defines the following stylesheet that applies (directly or via inheritance) to my custom class:
QAbstractButton { font-weight: bold; background-color: red }
QAbstractButton:checked { background-color: blue }
In my paintEvent
method, how do I get the correct background color to show up for the checked state?
void MyButton::paintEvent(QPaintEvent */*event*/) {
ensurePolished(); // Don't think this is necessary...
qDebug() << Q_FUNC_INFO << isChecked(); // This is showing the right value
QStylePainter painter(this);
painter.fillRect(rect(), painter.background()); // always red, even if checked
}
I assume I have to something like:
if (isChecked()) {
// painter.fillRect(rect(), ???);
//
// style()->drawPrimitive(???, ...);
//
// QStyleOptionButton opt;
// opt.initFrom(this);
// QBrush bg_brush = opt.???
// painter.fillRect(rect(), bg_brush);
//
// ???
} else {
painter.fillRect(rect(), painter.background());
}
How do I get the brush to use for the checked-state background that Qt resolved from the style sheets?