Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painting custom QWidget based on style sheet pseudo-state value

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?

like image 373
Dave Mateer Avatar asked May 02 '11 23:05

Dave Mateer


1 Answers

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.

like image 172
Dave Mateer Avatar answered Nov 03 '22 17:11

Dave Mateer