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

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(Corrected Style)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:snippets]]
[[Category:HowTo]]
[[Category:HowTo]]
__NOEDITSECTION__
__NOTOC__


'''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]]
'''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]]


= How to Change the Background Color of QWidget =
== Introduction ==


[http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget] 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.
{{DocLink|QWidget}} 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 [http://doc.qt.io/qt-5.0/qtgui/qpalette.html QPalette]
The first example demonstrates how to change the background color using {{DocLink|QPalette}}


<code>
<code>
Line 18: Line 17:
m_pMyWidget->setGeometry(0,0,300,100);
m_pMyWidget->setGeometry(0,0,300,100);
QPalette Pal(palette());
QPalette Pal(palette());
// set black background
// set black background
Pal.setColor(QPalette::Background, Qt::black);
Pal.setColor(QPalette::Background, Qt::black);
Line 27: Line 27:
== Using Style Sheet ==
== Using Style Sheet ==


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].
The style sheet contains a textual description of customizations to the widget's style, as described in the [http://doc.qt.io/qt-5/stylesheet.html Qt Style Sheets document].


<code>
<code>
Line 36: Line 36:
</code>
</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 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 :


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

Revision as of 09:35, 4 March 2015



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

Introduction

QWidget 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

m_pMyWidget = new QWidget(this);
m_pMyWidget->setGeometry(0,0,300,100);
QPalette Pal(palette());

// set black background
Pal.setColor(QPalette::Background, Qt::black);
m_pMyWidget->setAutoFillBackground(true);
m_pMyWidget->setPalette(Pal);
m_pMyWidget->show();

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.

m_pMyWidget = new QWidget(this);
m_pMyWidget->setGeometry(0,0,300,100);
m_pMyWidget->setStyleSheet("background-color:black;");
m_pMyWidget->show();

Both ways to change the background color of QWidget have been successfully built using Qt SDK 1.1 and tested on Symbian 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);
}