QWidget Semi-transparent Background Color: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[QWidget Semi-transparent Background Color Bulgarian|Български]]
[[Category:snippets]]<br />[[Category:HowTo]]


=QWidget Semi-transparent Background Color=
'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]]


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]''.
= QWidget Semi-transparent Background Color =
 
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.


* 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 [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]''
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.


===Categories:===
== See Also ==


* [[:Category:HowTo|HowTo]]
&quot;How to Change the Background Color of QWidget&amp;quot;:http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget
* [[:Category:snippets|snippets]]

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&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

.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&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