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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(QPalette::Background is obsolete (cf https://doc.qt.io/qt-5/qpalette.html))
(9 intermediate revisions by 6 users not shown)
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]]
{{LangSwitch}}
[[Category:HowTo]]
__NOEDITSECTION__
__NOTOC__
==Introduction==


=How to Change the Background Color of QWidget=
{{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.


[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.
==Using the Palette==


==Using the Palette==
The first example demonstrates how to change the background color using {{DocLink|QPalette}}
 
<code>m_myWidget = new QWidget(this);
m_myWidget->setGeometry(0, 0, 300, 100);
QPalette pal = QPalette();


The first example demonstrates how to change the background color using [http://doc.qt.io/qt-5.0/qtgui/qpalette.html QPalette] ''[qt.io]''
// set black background</code>
pal.setColor(QPalette::Window, Qt::black);
m_myWidget->setAutoFillBackground(true);
m_myWidget->setPalette(pal);
m_myWidget->show();</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 [http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html Qt Style Sheets document] ''[qt.io]''.
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].


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.
<code>
m_myWidget = new QWidget(this);
m_myWidget->setGeometry(0, 0, 300, 100);
m_myWidget->setStyleSheet("background-color:black;");
m_myWidget->show();
</code>
 
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 :


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


* [[:Category:HowTo|HowTo]]
}
* [[:Category:snippets|snippets]]
</code>

Revision as of 21:43, 7 August 2021

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh


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_myWidget = new QWidget(this);
m_myWidget->setGeometry(0, 0, 300, 100);
QPalette pal = QPalette();

// set black background

pal.setColor(QPalette::Window, Qt::black); m_myWidget->setAutoFillBackground(true); m_myWidget->setPalette(pal); m_myWidget->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_myWidget = new QWidget(this);
m_myWidget->setGeometry(0, 0, 300, 100);
m_myWidget->setStyleSheet("background-color:black;");
m_myWidget->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* event)
{
 QStyleOption opt;
 opt.init(this);
 QPainter p(this);
 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

 QWidget::paintEvent(event);

}