QWidget Semi-transparent Background Color: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(clean-up)
 
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:snippets]]
[[Category:snippets]]
[[Category:HowTo]]
[[Category:HowTo]]
'''English''' [[QWidget_Semi-transparent_Background_Color_Bulgarian|Български]]
= QWidget Semi-transparent Background Color =
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
Declare overload of paintEvent in .h file...


.h file
<code>
<code>
protected:
protected:
 
  //overload from QWidget
//overload from QWidget
  void paintEvent(QPaintEvent* event);
void paintEvent(QPaintEvent* event);
</code>
</code>


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


.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>
The code snippet is inspired by solution proposed by '''Antonio Di Monaco''' at [http://developer.qt.nokia.com/forums/viewthread/1488 this forum thread].


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

See Also

How to Change the Background Color of QWidget