widget.h:
#ifndef WIDGET_H #define WIDGET_H #include<QPaintEvent> #include <QWidget> #include<QPixmap> #include<QImage> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); void paintEvent(QPaintEvent *e); private: QPixmap *qp; QImage *ql; }; #endif // WIDGET_H
widget.cpp:
#include "widget.h" #include<QPainter> Widget::Widget(QWidget *parent) : QWidget(parent) { //qp=new QPixmap("C:/Users/admin/Desktop/QT_Project/untitled15/1.png"); ql=new QImage("C:/Users/admin/Desktop/QT_Project/untitled15/1.png"); } void Widget::paintEvent(QPaintEvent *e) { QPainter paint; paint.begin(this); // paint.drawPixmap(0,0,qp->scaled(this->width(),this->height())); paint.drawImage(0,0,ql->scaled(this->width(),this->height())); paint.end(); } Widget::~Widget() { delete qp; }
main.cpp:
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
效果: