#include #include #include #include #include #include #include class PainterWidget: public QWidget { protected: void paintEvent(QPaintEvent *); }; void PainterWidget::paintEvent(QPaintEvent *event) { /************************ 图片随窗体改变而改变 ************************/ QLabel *label = new QLabel; label->setPixmap(QPixmap("c:/painterwidget/images/house.png ")); label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); //忽视初始图片值 label->setScaledContents(true); //设置可改变的内容 QScrollArea *scroll=new QScrollArea; scroll->setWidgetResizable(true); scroll->setWidget(label); QHBoxLayout *main=new QHBoxLayout; main->addWidget(scroll); setLayout(main); } int main(int argc,char **argv) { QApplication app(argc,argv); PainterWidget pWidget; pWidget.resize(400,400); pWidget.show(); return app.exec(); }