Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

QWidget Semi-transparent Background Color

From Qt Wiki
Jump to navigation Jump to 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