上一篇文章中讲了,用label中显示QPixmap类的方法,像素的操作通常用QImage类进行操作,本篇展示QImage类的简单代码
//imagelabel.h
#ifndef IMAGELABEL_H
#define IMAGELABEL_H
#include <QWidget>
#include <QLabel>
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QString>
#include <QDebug>
#include <QObject>
#include <QPaintEvent>
class ImageLabel:public QLabel
{
Q_OBJECT
public:
explicit ImageLabel(QString path,QWidget *parent = 0);
~ImageLabel();
void paintEvent(QPaintEvent* event);
private:
QImage* image;
};
#endif // IMAGELABEL_H
//imagelabel.cpp
#include "imagelabel.h"
ImageLabel::ImageLabel(QString path,QWidget *parent):QLabel(parent)
{
image= new QImage(path);
}
ImageLabel::~ImageLabel(){
delete image;
}
void ImageLabel::paintEvent(QPaintEvent* event){
QPainter painter(this);
painter.drawImage(rect(),*image);
}
下面是MainWindow中调用该类的代码
QString path="H:\\project\\images\\bay_night.jpg";
ImageLabel* label= new ImageLabel(path,this);
label->resize(1600,900);
由于考虑到以后图像的实时变化,所以使用了QPainter类来显示
下为效果图