QWidget Semi-transparent Background Color: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Convert ExpressionEngine links) |
(clean-up) |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[[Category:HowTo]] | [[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()]. | 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> | <code> | ||
protected: | protected: | ||
//overload from QWidget | |||
//overload from QWidget | void paintEvent(QPaintEvent* event); | ||
</code> | </code> | ||
Implement the overload of paintEvent in.cpp file... | |||
<code> | <code> | ||
void MyWidget::paintEvent(QPaintEvent* /*event*/) | void MyWidget::paintEvent(QPaintEvent* /*event*/) { | ||
{ | |||
QColor backgroundColor = palette().light().color(); | QColor backgroundColor = palette().light().color(); | ||
backgroundColor.setAlpha(200); | backgroundColor.setAlpha(200); | ||
Line 32: | Line 21: | ||
} | } | ||
</code> | </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] | [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);
}