How to Change the Background Color of QWidget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' | [[How to Change the Background Color of QWidget German|Deutsch]] | [[How to Change the Background Color of QWidget Bulgarian|Български]] | [[How to Change the Background Color of QWidget Japanese|日本語]] | [[How to Change the Background Color of QWidget Spanish|Español]] | [[How to Change the Background Color of QWidget SimplifiedChinese|简体中文]]| [[How to Change the Background Color of QWidget Persian|Persian]]
[[Category:snippets]]<br />[[Category:HowTo]]


=How to Change the Background Color of QWidget=
'''English''' | [[How_to_Change_the_Background_Color_of_QWidget_German|Deutsch]] | [[How_to_Change_the_Background_Color_of_QWidget_Bulgarian|Български]] | [[How_to_Change_the_Background_Color_of_QWidget_Japanese|日本語]] | [[How_to_Change_the_Background_Color_of_QWidget_Spanish|Español]] | [[How_to_Change_the_Background_Color_of_QWidget_SimplifiedChinese|简体中文]]| [[How_to_Change_the_Background_Color_of_QWidget_Persian|Persian]]


[http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget] ''[qt.io]'' is the base class of all user interface objects which means that the same approaches for changing the background color can be used with them too.
= How to Change the Background Color of QWidget =


==Using the Palette==
&quot;QWidget&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html is the base class of all user interface objects which means that the same approaches for changing the background color can be used with them too.


The first example demonstrates how to change the background color using [http://doc.qt.io/qt-5.0/qtgui/qpalette.html QPalette] ''[qt.io]''
== Using the Palette ==


==Using Style Sheet==
The first example demonstrates how to change the background color using &quot;QPalette&amp;quot;:http://doc.qt.io/qt-5.0/qtgui/qpalette.html


The style sheet contains a textual description of customizations to the widget’s style, as described in the [http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html Qt Style Sheets document] ''[qt.io]''.
<code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget-&gt;setGeometry(0,0,300,100);<br />QPalette Pal(palette());<br />// set black background<br />Pal.setColor(QPalette::Background, Qt::black);<br />m_pMyWidget-&gt;setAutoFillBackground(true);<br />m_pMyWidget-&gt;setPalette(Pal);<br />m_pMyWidget-&gt;show();<br /></code>


Both ways to change the background color of QWidget have been successfully built using Qt <span class="caps">SDK</span> 1.1 and tested on Symbian^3 devices.
== Using Style Sheet ==
 
The style sheet contains a textual description of customizations to the widget's style, as described in the &quot;Qt Style Sheets document&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html.
 
<code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget-&gt;setGeometry(0,0,300,100);<br />m_pMyWidget-&gt;setStyleSheet(&quot;background-color:black;&quot;);<br />m_pMyWidget-&gt;show();<br /></code>
 
Both ways to change the background color of QWidget have been successfully built using Qt SDK 1.1 and tested on Symbian^3 devices.


'''Note:''' If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :
'''Note:''' If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :


===Categories:===
<code>void CustomWidget::paintEvent(QPaintEvent *)<br /> {<br /> QStyleOption opt;<br /> opt.init(this);<br /> QPainter p(this);<br /> style()-&gt;drawPrimitive(QStyle::PE_Widget, &amp;opt, &amp;p, this);
 
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]

Revision as of 14:11, 23 February 2015


English | Deutsch | Български | 日本語 | Español | 简体中文| Persian

How to Change the Background Color of QWidget

"QWidget&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html is the base class of all user interface objects which means that the same approaches for changing the background color can be used with them too.

Using the Palette

The first example demonstrates how to change the background color using "QPalette&quot;:http://doc.qt.io/qt-5.0/qtgui/qpalette.html

<br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget-&gt;setGeometry(0,0,300,100);<br />QPalette Pal(palette());<br />// set black background<br />Pal.setColor(QPalette::Background, Qt::black);<br />m_pMyWidget-&gt;setAutoFillBackground(true);<br />m_pMyWidget-&gt;setPalette(Pal);<br />m_pMyWidget-&gt;show();<br />

Using Style Sheet

The style sheet contains a textual description of customizations to the widget's style, as described in the "Qt Style Sheets document&quot;:http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html.

<br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget-&gt;setGeometry(0,0,300,100);<br />m_pMyWidget-&gt;setStyleSheet(&quot;background-color:black;&quot;);<br />m_pMyWidget-&gt;show();<br />

Both ways to change the background color of QWidget have been successfully built using Qt SDK 1.1 and tested on Symbian^3 devices.

Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :

void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);