QWidget Semi-transparent Background Color: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(clean-up) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[[Category:HowTo]] | |||
This code snippet shows how to make the background color of [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget] semi-transparent by overloading [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#paintEvent paintEvent()]. | |||
Declare overload of paintEvent in .h file... | |||
<code> | |||
protected: | |||
//overload from QWidget | |||
void paintEvent(QPaintEvent* event); | |||
</code> | |||
Implement the overload of paintEvent in.cpp file... | |||
<code> | |||
void MyWidget::paintEvent(QPaintEvent* /*event*/) { | |||
QColor backgroundColor = palette().light().color(); | |||
backgroundColor.setAlpha(200); | |||
QPainter customPainter(this); | |||
customPainter.fillRect(rect(),backgroundColor); | |||
} | |||
</code> | |||
== See Also == | == See Also == | ||
[http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget How to Change the Background Color of QWidget] |
Latest revision as of 13:52, 24 March 2016
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);
}