//imagelabel.h
#ifndef IMAGELABEL_H
#define IMAGELABEL_H
#include <QWidget>
#include <QLabel>
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QString>
#include <QDebug>
#include <QObject>
class ImageLabel:public QLabel
{
Q_OBJECT
public:
explicit ImageLabel(QString path,QWidget *parent = 0);
~ImageLabel();
private:
};
#endif // IMAGELABEL_H
//imagelabel.cpp
#include "imagelabel.h"
ImageLabel::ImageLabel(QString path,QWidget *parent):QLabel(parent)
{
this->resize(800,600);
QPixmap* pix=new QPixmap(path);
qDebug()<<path;
this->setPixmap(pix->scaled(size()));
}
ImageLabel::~ImageLabel(){
}
在MainWindow中以以下代码进行调用
QString path="H:\\project\\images\\bay_night.jpg";
ImageLabel* label= new ImageLabel(path,this);
以上代码能实现在主窗口上显示路径中的图片了,但在编辑的时候,不用new的方法定义ImageLabel类就不能正常显示,望小伙伴们能解释一下。
不过上述代码用的是QPixmap,不利于直接进行像素操作,下一篇文章中会做下修改。