您的位置:首页 > 编程语言 > Qt开发

qt 之 How to Change the Background Color of QWidget

2014-11-03 16:04 423 查看


Using the Palette

The first example demonstrates how to change the background color using QPalette [qt-project.org]

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 [qt-project.org].

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^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);

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: