QWidget Semi-transparent Background Color: Difference between revisions

From Qt Wiki
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]]<br />[[Category:HowTo]]
[[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()].


'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]]
Declare overload of paintEvent in .h file...


= QWidget Semi-transparent Background Color =
<code>
protected:
  //overload from QWidget
  void paintEvent(QPaintEvent* event);
</code>


This code snippet shows how to make the background color of &quot;QWidget&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html semi-transparent by overloading &quot;paintEvent()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#paintEvent.
Implement the overload of paintEvent in.cpp file...


* Declare overload of paintEvent
<code>
 
void MyWidget::paintEvent(QPaintEvent* /*event*/) {
.h file<br /><code><br />protected:
QColor backgroundColor = palette().light().color();
 
backgroundColor.setAlpha(200);
//overload from QWidget<br /> void paintEvent(QPaintEvent* event);<br /></code>
QPainter customPainter(this);
 
customPainter.fillRect(rect(),backgroundColor);
* Implement the overload of paintEvent
}
 
</code>
.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 &quot;this forum thread&amp;quot;:http://developer.qt.nokia.com/forums/viewthread/1488.


== See Also ==
== See Also ==


&quot;How to Change the Background Color of QWidget&amp;quot;:http://wiki.qt.io/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);
}

See Also

How to Change the Background Color of QWidget