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()
{

}
相关推荐
钱彬 (Qian Bin)19 小时前
项目实践11—全球证件智能识别系统(切换为PostgreSQL数据库)
人工智能·qt·fastapi
深蓝海拓20 小时前
PySide6从0开始学习的笔记(二) 控件(Widget)之容器类控件
笔记·qt·学习·pyqt
_OP_CHEN21 小时前
【从零开始的Qt开发指南】(十一)Qt常用控件之多元素控件与容器类控件深度解析
开发语言·qt·前端开发·多元素控件·gui开发·qt常用控件·容器类控件
我送炭你添花1 天前
Pelco KBD300A 模拟器:06.用 PyQt5 实现 1:1 像素级完美复刻 Pelco KBD300A 键盘
python·qt·自动化·运维开发
大道随心1 天前
【QT】自动化设备控制界面搭建
开发语言·qt·自动化
深蓝海拓1 天前
PySide6从0开始学习的笔记(六) 控件(Widget)之按钮类
笔记·python·qt·学习·pyqt
深蓝海拓1 天前
PySide6从0开始学习的笔记(七) 控件(Widget)之文字输入类控件
笔记·python·qt·学习·pyqt
郝学胜-神的一滴1 天前
使用Qt OpenGL开发俄罗斯方块:从零到一实现经典游戏
c++·qt·程序人生·游戏·设计模式·系统架构·图形渲染
海涛高软1 天前
Qt菜单项切换主界面
开发语言·qt
水煎包V:YEDIYYDS8881 天前
QT 在 QML中 嵌入显示qwidget界面显示的两种方式,已在项目中验证
qt·qml·qt开发·qwidget