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?
I never could find out how to get the resolved color (and padding) information, but was able to work around it by painting sub-elements of other widgets into mine. This isn't exactly what I was trying to do, and may not work in other cases (if your widget can't be composed by mashing together stuff that Qt does know how to draw).
void MyButton::paintEvent(QPaintEvent */*event*/) {
QStylePainter painter(this);
QStyleOptionButton opt;
opt.initFrom(this);
opt.state |= isChecked() ? QStyle::State_On : QStyle::State_Off;
opt.text = text();
painter.drawPrimitive(QStyle::PE_Widget, opt);
QStyleOptionButton label_opt = opt;
label_opt.rect =
style()->subElementRect(QStyle::SE_CheckBoxContents, &opt, this);
painter.drawControl(QStyle::CE_CheckBoxLabel, label_opt);
// ... etc.
}
I still think there has to be a better way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With