QT 编程怎么让图像跟着窗体的大小而变化
RT
那位大侠教教 本人刚刚接触 谢谢了
[解决办法]
重载窗体的resizeEvent函数,然后再画一遍。
[解决办法]
Quote:#ifndef IMAGEWIDGET_HPP#define IMAGEWIDGET_HPP#include <QtCore>#include <QtGui>class ImageWidget : public QWidget{Q_OBJECTpublic: ImageWidget(QWidget *parent = 0, Qt::WindowFlags f = 0); virtual ~ImageWidget();protected: void resizeEvent(QResizeEvent *event);private: QImage _image;};#endifCPP文件: ImageWidget.cppQuote:#include "ImageWidget.hpp"ImageWidget::ImageWidget(QWidget *parent, Qt::WindowFlags f): QWidget(parent, f){ _image.load("image/image_background"); setAutoFillBackground(true); // 这个属性一定要设置 QPalette pal(palette()); pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); setPalette(pal);}ImageWidget::~ImageWidget(){}// 随着窗体变化而设置背景void ImageWidget::resizeEvent(QResizeEvent *event){ QWidget::resizeEvent(event); QPalette pal(palette()); pal.setBrush(QPalette::Window, QBrush(_image.scaled(event->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); setPalette(pal);}