In QT, qpixmap is used to represent an image. It supports loading in PNG and jpg formats.
Qpixmap PM ("C:/test.png"); or qpixmap PM; PM. Load ("C:/test/PNG ");
In QT, there are two image paths:
(1) images in the file system: use absolute or relative paths.
(2) files in the resource: Start with a colon, for example,/test/source/logo.jpg
Drawn parameters:
(1) source rectangle
You can draw all or part of a graph.
QRect source(0,0,ima_width,img_height);
(2) Target rectangle
It can be filled in all windows, or only part of the window can be filled.
QRect target(0,0,width/2,height/2);
Called functions:
painter.drawPixmap(target,m_picture,source)
Circlewidget. h
#ifndef CIRCLAWIDGET_H#define CIRCLAWIDGET_H#include <QFrame>#include<QTimer>#include<QPixmap>class CircleWidget:public QFrame{ Q_OBJECTpublic: CircleWidget(QWidget *parent); ~CircleWidget();private: void paintEvent(QPaintEvent *event);private: QPixmap m_picture;};#endif // CIRCLAWIDGET_H
Circlewidget. cpp
# include"circlewidget.h"#include<QPainter>CircleWidget::CircleWidget(QWidget *parent):QFrame(parent){ m_picture.load("/home/jun/untitled1/backimage.PNG");}CircleWidget::~CircleWidget(){}void CircleWidget::paintEvent(QPaintEvent *event){ QPainter painter(this); int width=this->width(); int height=this->height(); QRect target(0,0,width,height); int img_width=m_picture.width(); int img_height=m_picture.height(); QRect source (0,0,img_width,img_height); painter.drawPixmap(target,m_picture,source);}
Result:
Window painting in QT-image painting