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

How to Change the Background Color of QWidget

QWidget [doc.qt.nokia.com] is the base class of all user interface objects which means that the same approaches for changing the background color can be use with them too.

Using the Palette

The first example demonstrates how to change the background color using QPalette [doc.qt.nokia.com]

  1. m_pMyWidget = new QWidget(this);
  2. m_pMyWidget->setGeometry(0,0,300,100);
  3. QPalette Pal(palette());
  4. // set black background
  5. Pal.setColor(QPalette::Background, Qt::black);
  6. m_pMyWidget->setAutoFillBackground(true);
  7. m_pMyWidget->setPalette(Pal);
  8. 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 [doc.qt.nokia.com].

  1. m_pMyWidget = new QWidget(this);
  2. m_pMyWidget->setGeometry(0,0,300,100);
  3. m_pMyWidget->setStyleSheet("background-color:black;");
  4. 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^3 devices.

Categories: