代碼是在前篇空間的代碼基礎上加的,給主窗口添加背景顏色或背景圖片
直接上代碼:
int main (int argc, char **argv)
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Enter Your Age");
/* set background color */
QPalette palette(window->palette());
palette.setColor(QPalette::Background, Qt::red);
window->setPalette(palette);
/* set background picture <1> */
#if 0
window->setAutoFillBackground(true);
QPalette palette;
QPixmap pixmap("../picture/1.png");
palette.setBrush(QPalette::Window, QBrush(pixmap));
window->setPalette(palette);
#endif
/* set background piture <2> */
#if 0
QPixmap pixmap = QPixmap("../picture/1.png").scaled(window->size());
QPalette palette(window->palette());
palette.setBrush(QPalette::Background, QBrush(pixmap));
window->setPalette(palette);
#endif
QSpinBox *spinbox = new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);
spinbox->setRange(0,130);
slider->setRange(0,130);
QObject::connect(spinbox, SIGNAL(valueChanged(int)),
slider,SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)),
spinbox,SLOT(setValue(int)));
spinbox->setValue(35);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinbox);
layout->addWidget(slider);
window->setLayout(layout);
window->show();
return app.exec();
} /* ----- End of main() ----- */
代碼中/*set background color*/ 下的代碼是設置背景顏色,./*set background picture*/是設置背景圖片,有兩種方法
借用別人的話:
<1>以上都是用scaled的方式對圖片進行適應圖片的大小,因此所給的圖片不一定滿足要求.圖片採用此方式要注意(具體該怎麼解決還沒去研究,以後有空補上)
有很多東西沒研究,今天就這樣了.
參考博客:http://www.cppblog.com/qianqian/archive/2010/07/25/121238.html