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:
[[Category:snippets]]<br />[[Category:HowTo]]
[[Category:snippets]]
[[Category:HowTo]]


'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]]
'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]]
Line 5: Line 6:
= QWidget Semi-transparent Background Color =
= 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.
This code snippet shows how to make the background color of "QWidget":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
* Declare overload of paintEvent


.h file<br /><code><br />protected:
.h file
<code>
protected:


//overload from QWidget<br /> void paintEvent(QPaintEvent* event);<br /></code>
//overload from QWidget
void paintEvent(QPaintEvent* event);
</code>


* Implement the overload of paintEvent
* Implement the overload of paintEvent


.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>
.cpp file
<code>
void MyWidget::paintEvent(QPaintEvent* /*event*/)
{
QColor backgroundColor = palette().light().color();
backgroundColor.setAlpha(200);
QPainter customPainter(this);
customPainter.fillRect(rect(),backgroundColor);
}
</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.
The code snippet is inspired by solution proposed by '''Antonio Di Monaco''' at "this forum thread":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
"How to Change the Background Color of QWidget":http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget

Revision as of 09:23, 25 February 2015


English Български

QWidget Semi-transparent Background Color

This code snippet shows how to make the background color of "QWidget":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

protected:

//overload from QWidget
 void paintEvent(QPaintEvent* event);
  • Implement the overload of paintEvent

.cpp file

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

The code snippet is inspired by solution proposed by Antonio Di Monaco at "this forum thread":http://developer.qt.nokia.com/forums/viewthread/1488.

See Also

"How to Change the Background Color of QWidget":http://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget