QWidget Semi-transparent Background Color: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(clean-up)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''English''' [[QWidget Semi-transparent Background Color Bulgarian|Български]]
[[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()].


=QWidget Semi-transparent Background Color=
Declare overload of paintEvent in .h file...


This code snippet shows how to make the background color of [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget] ''[qt.io]'' semi-transparent by overloading [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#paintEvent paintEvent()] ''[qt.io]''.
<code>
protected:
  //overload from QWidget
  void paintEvent(QPaintEvent* event);
</code>


* Declare overload of paintEvent
Implement the overload of paintEvent in.cpp file...


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


* Implement the overload of paintEvent
== See Also ==


.cpp file<br />
[http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget How to Change the Background Color of QWidget]
 
The code snippet is inspired by solution proposed by '''Antonio Di Monaco''' at [http://developer.qt.nokia.com/forums/viewthread/1488 this forum thread] ''[developer.qt.nokia.com]''.
 
==See Also==
 
[[How to Change the Background Color of QWidget]] ''[qt.io]''
 
===Categories:===
 
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]

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);
}

See Also

How to Change the Background Color of QWidget