QLable提升类

实现:

1.图片移动、保持纵横比缩放、右键菜单

1.myLabel.h

cpp 复制代码
#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    MyLabel(QWidget *parent = nullptr);
private:
    void contextMenuEvent(QContextMenuEvent* e) override;
    void mousePressEvent(QMouseEvent* e) override;
    void mouseReleaseEvent(QMouseEvent* e) override;
    void mouseMoveEvent(QMouseEvent* e) override;

    void paintEvent(QPaintEvent* e) override;
    void wheelEvent(QWheelEvent* e) override;


    double m_zoomValue = 1.0;


    bool isPress = false;

    int m_XPtInterval = 0;
    int m_YPtInterval = 0;
    QPoint m_OldPos;

private slots:
    void onLoadImage(void);
    void onZoomInImage(void);
    void onZoomOutImage(void);
    void onPresetImage(void);

};

#endif // MYLABEL_H

2.myLabel.cpp

cpp 复制代码
#include "myLabel.h"

#include <QMenu>
#include <QPaintEvent>
#include <QPainter>
#include <QWheelEvent>
MyLabel::MyLabel(QWidget *parent):
    QLabel(parent)
{
    // setScaledContents(true);
    // this->setAlignment(Qt::AlignCenter);
    
    //如果图片太大,这个可以保证图片不会放大
    //setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);

}

void MyLabel::contextMenuEvent(QContextMenuEvent *e)
{
    QPoint pos = e->pos();
    pos = this->mapToGlobal(pos);
    QMenu *menu = new QMenu(this);

    QAction *loadImage = new QAction(tr("图片放大"));
    QObject::connect(loadImage, &QAction::triggered, this, &MyLabel::onLoadImage);
    menu->addAction(loadImage);
    // menu->addSeparator();

    QAction *zoomInAction = new QAction(tr("图片居中还原"));
    QObject::connect(zoomInAction, &QAction::triggered, this, &MyLabel::onZoomInImage);
    menu->addAction(zoomInAction);

    QAction *zoomOutAction = new QAction(tr("3"));
    QObject::connect(zoomOutAction, &QAction::triggered, this, &MyLabel::onZoomOutImage);
    menu->addAction(zoomOutAction);

    QAction *presetAction = new QAction(tr("4"));
    QObject::connect(presetAction, &QAction::triggered, this, &MyLabel::onPresetImage);
    menu->addAction(presetAction);

    menu->exec(pos);
    // menu->show();

}

void MyLabel::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton){
        isPress = true;
        m_OldPos = e->pos();
    }
}

void MyLabel::mouseReleaseEvent(QMouseEvent *e)
{
    isPress = false;
    this->setCursor(Qt::ArrowCursor);
}

void MyLabel::mouseMoveEvent(QMouseEvent *e)
{
    if (!isPress)
        return QWidget::mouseMoveEvent(e);

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


    m_XPtInterval += xPtInterval;
    m_YPtInterval += yPtInterval;

    m_OldPos = pos;

    this->update();
}

void MyLabel::paintEvent(QPaintEvent* e){
    if(this->pixmap().isNull())
    {
        return QLabel::paintEvent(e);
    }
    QPainter painter(this);
    int width = qMin(pixmap().width(), this->width());
    int height = width * 1.0 / (pixmap().width() * 1.0 /pixmap().height());
    height = qMin(height, this->height());
    width = height * 1.0 * (pixmap().width() * 1.0 / pixmap().height());

    painter.translate(this->width() / 2+m_XPtInterval, this->height() / 2+m_YPtInterval );

    painter.scale(m_zoomValue,m_zoomValue);

    QRect rect(-width/2,-height/2,width,height);
    painter.drawImage(rect,this->pixmap().toImage());
    // return QLabel::paintEvent(e);
}

void MyLabel::wheelEvent(QWheelEvent *e)
{

    int p = e->angleDelta().y();
    if(p>0)
    {
        m_zoomValue += 0.1;
        update();
    }
    if(p<0){
        m_zoomValue -= 0.1;
        update();
    }

}

void MyLabel::onLoadImage()
{
    MyLabel* label = new MyLabel();
    label->setWindowTitle(parent()->objectName());
    label->setPixmap(pixmap());
    label->showMaximized();

}

void MyLabel::onZoomInImage()
{
    m_XPtInterval = 0;
    m_YPtInterval = 0;
    m_zoomValue = 1.0;
    update();
}

void MyLabel::onZoomOutImage()
{

}

void MyLabel::onPresetImage()
{

}
相关推荐
7yewh1 小时前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
mahuifa13 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
冷眼看人间恩怨13 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
云空18 小时前
《QT 5.14.1 搭建 opencv 环境全攻略》
开发语言·qt·opencv
小老鼠不吃猫19 小时前
力学笃行(二)Qt 示例程序运行
开发语言·qt
晓纪同学20 小时前
QT创建一个模板槽和信号刷新UI
开发语言·qt·ui
爱码小白1 天前
PyQt5 学习方法之悟道
开发语言·qt·学习方法
人才程序员1 天前
QML z轴(z-order)前后层级
c语言·前端·c++·qt·软件工程·用户界面·界面
学习BigData1 天前
【使用PyQt5和YOLOv11开发电脑屏幕区域的实时分类GUI】——选择检测区域
qt·yolo·分类
yerennuo2 天前
FFmpeg库之ffmpeg
qt·ffmpeg