QWidget Semi-transparent Background Color

From Qt Wiki
Revision as of 08:54, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


English Български

QWidget Semi-transparent Background Color

This code snippet shows how to make the background color of "QWidget":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html semi-transparent by overloading "paintEvent()":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#paintEvent.

  • Declare overload of paintEvent

.h file

<br />protected:

//overload from QWidget<br /> void paintEvent(QPaintEvent* event);<br />
  • Implement the overload of paintEvent

.cpp file

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

The code snippet is inspired by solution proposed by Antonio Di Monaco at "this forum thread&quot;:http://developer.qt.nokia.com/forums/viewthread/1488.

See Also

"How to Change the Background Color of QWidget&quot;:http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget