QWidget Semi-transparent Background Color

From Qt Wiki
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