2.在QLabel中缩放移动图片

项目需要使用图片缩放功能,但在网上没有找到现成的代码,结合几个博客,实现了功能,在此记录一下.
QPainter可以在QWidget上进行绘制图形,但是在QWidget的子类QLabel上绘制图形就会出错,所以采用自定义QLabel的方法.

一、创建项目
2.在QLabel中缩放移动图片_第1张图片
二、自定义QLabel类MyLabel
1.添加新文件
2.在QLabel中缩放移动图片_第2张图片
2.mylabel.h

 #ifndef MYLABEL_H
#define MYLABEL_H

#include 
#include 
class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = 0);

protected:
    void contextMenuEvent(QContextMenuEvent *event) override;   //右键菜单
    void paintEvent(QPaintEvent *event);                        //QPaint画图
    void wheelEvent(QWheelEvent *event) override;               //鼠标滚轮滚动
    void mousePressEvent(QMouseEvent *event) override;          //鼠标摁下
    void mouseMoveEvent(QMouseEvent *event) override;           //鼠标松开
    void mouseReleaseEvent(QMouseEvent *event) override;        //鼠标发射事件

private slots:
    void OnSelectImage();       //选择打开图片
    void OnZoomInImage();       //图片放大
    void OnZoomOutImage();      //图片缩小
    void OnPresetImage();       //图片还原

private:
    QImage Image;           //显示的图片
    qreal ZoomValue = 1.0;  //鼠标缩放值
    int XPtInterval = 0;    //平移X轴的值
    int YPtInterval = 0;    //平移Y轴的值
    QPoint OldPos;          //旧的鼠标位置
    bool Pressed = false;   //鼠标是否被摁压
    QString LocalFileName;  //图片名
};

#endif // MYLABEL_H

3.mylabel.cpp

#include "mylabel.h"
#include 
#include 
#include 
#include 
#include 

MyLabel::MyLabel(QWidget *parent):QLabel(parent)
{

}
//QPainter画图
void MyLabel::paintEvent(QPaintEvent *event)
{
    if (LocalFileName.isNull())
        return QWidget::paintEvent(event);
        
    Image.load(LocalFileName);
    QPainter painter(this);

    // 根据窗口计算应该显示的图片的大小
    int width = qMin(Image.width(), this->width());
    int height = width * 1.0 / (Image.width() * 1.0 / Image.height());
    height = qMin(height, this->height());
    width = height * 1.0 * (Image.width() * 1.0 / Image.height());

    // 平移
    painter.translate(this->width() / 2 + XPtInterval, this->height() / 2 + YPtInterval);

    // 缩放
    painter.scale(ZoomValue, ZoomValue);

    // 绘制图像
    QRect picRect(-width / 2, -height / 2, width, height);
    painter.drawImage(picRect, Image);
}
//鼠标滚轮滚动
void MyLabel::wheelEvent(QWheelEvent *event)
{
    int value = event->delta();
    if (value > 0)  //放大
        OnZoomInImage();
    else            //缩小
        OnZoomOutImage();

    update();
}
//鼠标摁下
void MyLabel::mousePressEvent(QMouseEvent *event)
{
    OldPos = event->pos();
    Pressed = true;
}
//鼠标松开
void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    if (!Pressed)
        return QWidget::mouseMoveEvent(event);

    this->setCursor(Qt::SizeAllCursor);
    QPoint pos = event->pos();
    int xPtInterval = pos.x() - OldPos.x();
    int yPtInterval = pos.y() - OldPos.y();

    XPtInterval += xPtInterval;
    YPtInterval += yPtInterval;

    OldPos = pos;
    update();
}
//鼠标发射事件
void MyLabel::mouseReleaseEvent(QMouseEvent */*event*/)
{
    Pressed = false;
    setCursor(Qt::ArrowCursor);
}
//选择打开图片
void MyLabel::OnSelectImage()
{
    LocalFileName = QFileDialog::getOpenFileName(this, "Open Image", "./", tr("Images (*.png *.xpm *.jpg)"));

    QFile file(LocalFileName);
    if (!file.exists())
        return;
    ZoomValue = 1.0;
    XPtInterval = 0;
    YPtInterval = 0;
    update();
}
//图片放大
void MyLabel::OnZoomInImage()
{
    ZoomValue += 0.05;
    update();
}
//图片缩小
void MyLabel::OnZoomOutImage()
{
    ZoomValue -= 0.05;
    if (ZoomValue <= 0)
    {
        ZoomValue = 0.05;
        return;
    }

    update();
}
//图片还原
void MyLabel::OnPresetImage()
{
    ZoomValue = 1.0;
    XPtInterval = 0;
    YPtInterval = 0;
    update();
}
//右键菜单
void MyLabel::contextMenuEvent(QContextMenuEvent *event)
{
    QPoint pos = event->pos();
    pos = this->mapToGlobal(pos);
    QMenu *menu = new QMenu(this);

    QAction *loadImage = new QAction(this);
    loadImage->setText("选择图片");
    connect(loadImage, &QAction::triggered, this, &MyLabel::OnSelectImage);
    menu->addAction(loadImage);
    menu->addSeparator();

    QAction *zoomInAction = new QAction(this);
    zoomInAction->setText("放大");
    connect(zoomInAction, &QAction::triggered, this, &MyLabel::OnZoomInImage);
    menu->addAction(zoomInAction);

    QAction *zoomOutAction = new QAction(this);
    zoomOutAction->setText("缩小");
    connect(zoomOutAction, &QAction::triggered, this, &MyLabel::OnZoomOutImage);
    menu->addAction(zoomOutAction);

    QAction *presetAction = new QAction(this);
    presetAction->setText("还原");
    connect(presetAction, &QAction::triggered, this, &MyLabel::OnPresetImage);
    menu->addAction(presetAction);

    menu->exec(pos);
}

三、提升QLabel
1.拖拽QLabel
回到主界面,拖拽一个QLabel,然后右键->布局->栅格布局
2.在QLabel中缩放移动图片_第3张图片
2.提升
选中TextLabel,右键->提升为…
2.在QLabel中缩放移动图片_第4张图片
填写信息,如下图,添加->提升
2.在QLabel中缩放移动图片_第5张图片
四、测试
运行程序,在界面上右击->选择图片,之后就可以通过滚轮放大缩小图片,长摁鼠标移动图片,还可以右键菜单操作图片。
2.在QLabel中缩放移动图片_第6张图片

你可能感兴趣的:(QT)