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

}
相关推荐
孤独得猿24 分钟前
Qt常用控件第一部分
服务器·开发语言·qt
強云6 小时前
界面架构- MVP(Qt)
qt·架构
嘤国大力士14 小时前
C++11&QT复习 (七)
java·c++·qt
嘤国大力士15 小时前
C++11&QT复习 (十一)
开发语言·c++·qt
wkm95615 小时前
qt.qpa.xcb: could not connect to display解决方法
开发语言·qt·ubuntu
追烽少年x18 小时前
在Qt中直接在构建目录下直接运行.exe文件报错问题分析
qt
AAA废品回收站陈师傅1 天前
19信号和槽_信号和槽的基本概念
qt
爱吃巧克力的程序媛1 天前
在 Qt 创建项目时,Qt Quick Application (Compat) 和 Qt Quick Application
开发语言·qt
AAA废品回收站陈师傅1 天前
18认识Qt坐标系
qt
m0_555762901 天前
QT 动态布局实现(待完善)
服务器·数据库·qt