How to Change the Background Color of QWidget: Difference between revisions
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
= How to Change the Background Color of QWidget = | = How to Change the Background Color of QWidget = | ||
"QWidget":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 == | == Using the Palette == | ||
The first example demonstrates how to change the background color using | The first example demonstrates how to change the background color using "QPalette":http://doc.qt.io/qt-5.0/qtgui/qpalette.html | ||
<code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget- | <code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget->setGeometry(0,0,300,100);<br />QPalette Pal(palette());<br />// set black background<br />Pal.setColor(QPalette::Background, Qt::black);<br />m_pMyWidget->setAutoFillBackground(true);<br />m_pMyWidget->setPalette(Pal);<br />m_pMyWidget->show();<br /></code> | ||
== Using Style Sheet == | == Using Style Sheet == | ||
The style sheet contains a textual description of customizations to the widget's style, as described in the | The style sheet contains a textual description of customizations to the widget's style, as described in the "Qt Style Sheets document":http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html. | ||
<code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget- | <code><br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget->setGeometry(0,0,300,100);<br />m_pMyWidget->setStyleSheet("background-color:black;");<br />m_pMyWidget->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. | Both ways to change the background color of QWidget have been successfully built using Qt SDK 1.1 and tested on Symbian^3 devices. | ||
Line 23: | Line 23: | ||
'''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 : | ||
<code>void CustomWidget::paintEvent(QPaintEvent *)<br /> {<br /> QStyleOption opt;<br /> opt.init(this);<br /> QPainter p(this);<br /> style()- | <code>void CustomWidget::paintEvent(QPaintEvent *)<br /> {<br /> QStyleOption opt;<br /> opt.init(this);<br /> QPainter p(this);<br /> style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); |
Revision as of 14:10, 24 February 2015
English | Deutsch | Български | 日本語 | Español | 简体中文| Persian
How to Change the Background Color of QWidget
"QWidget":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":http://doc.qt.io/qt-5.0/qtgui/qpalette.html
<br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget->setGeometry(0,0,300,100);<br />QPalette Pal(palette());<br />// set black background<br />Pal.setColor(QPalette::Background, Qt::black);<br />m_pMyWidget->setAutoFillBackground(true);<br />m_pMyWidget->setPalette(Pal);<br />m_pMyWidget->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":http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html.
<br />m_pMyWidget = new QWidget(this);<br />m_pMyWidget->setGeometry(0,0,300,100);<br />m_pMyWidget->setStyleSheet("background-color:black;");<br />m_pMyWidget->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);