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 QtStyle 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);
}
相关文章推荐
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- How to change the background color of an Edit Box
- how to change the background color of actionbar
- How To Change the Background Color of a Tab Control
- How to customize the background color of a UITableViewCell?
- How to change background color of gedit embedded terminal
- How to change the Text color of Menu item in Android?
- How to change the color of a CEikLabel
- How to customize the background color of a UITableViewCell? 修改UITableViewCell背景色
- How to change the output color of echo in Linux
- How to change the text color of the selected property sheet tab in Visual C++
- how to change the background of actionbar.tab
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- How to programmatically change the default view of an InfoPath form
- How to change the language of oracle sqlplus from chinese to english in windows(如何将oracle sql plus中的中文变成英文版)
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- 如何修改Screen背景?How to - Change the background color of a screen
- how to change text color of CEikEdwin
- How to change the name of logfile dynamically using log4j