2009-09-22

Change Qt Widget Background

Qt Widget's Background is changed by setPalette(QPalette);
The "Qt Tutorial 8 - Preparing for Battle" is changed: add a button to change the background color of ConnonField.
========
main.cpp:
========

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));

QPushButton *change = new QPushButton(tr("Change"));

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

LCDRange *angle = new LCDRange;
angle->setRange(5, 70);

CannonField *cannonField = new CannonField;

connect(angle, SIGNAL(valueChanged(int)),
cannonField, SLOT(setAngle(int)));
connect(cannonField, SIGNAL(angleChanged(int)),
angle, SLOT(setValue(int)));
connect(change, SIGNAL(clicked()),
cannonField, SLOT(changeBackground()));

...
}


===========
cannonfield.cpp
============
...
void CannonField::changeBackground()
{
setPalette(QPalette(QColor(0, 250, 200)));
update();

}
...



No comments:

Post a Comment