QWidget Semi-transparent Background Color: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[[Category:HowTo]] | |||
'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]] | |||
This code snippet shows how to make the background color of | = QWidget Semi-transparent Background Color = | ||
This code snippet shows how to make the background color of "QWidget&quot;: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 | * Declare overload of paintEvent | ||
.h file<br /> | .h file<br /><code><br />protected: | ||
//overload from QWidget<br /> void paintEvent(QPaintEvent* event);<br /></code> | |||
* Implement the overload of paintEvent | * Implement the overload of paintEvent | ||
.cpp file<br /> | .cpp file<br /><code><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 /></code> | ||
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 | |||
Revision as of 08:54, 24 February 2015
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":http://developer.qt.nokia.com/forums/viewthread/1488.
See Also
"How to Change the Background Color of QWidget":http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget