QWidget Semi-transparent Background Color

From Qt Wiki
Revision as of 13:52, 24 March 2016 by Wieland (talk | contribs) (clean-up)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This code snippet shows how to make the background color of QWidget semi-transparent by overloading paintEvent().

Declare overload of paintEvent in .h file...

protected:
  //overload from QWidget
  void paintEvent(QPaintEvent* event);

Implement the overload of paintEvent in.cpp file...

void MyWidget::paintEvent(QPaintEvent* /*event*/) {
 QColor backgroundColor = palette().light().color();
 backgroundColor.setAlpha(200);
 QPainter customPainter(this);
 customPainter.fillRect(rect(),backgroundColor);
}

See Also

How to Change the Background Color of QWidget