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

}
相关推荐
Mr.Q6 小时前
Qt多边形填充/不填充绘制
qt
可峰科技6 小时前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
7年老菜鸡8 小时前
策略模式(C++)三分钟读懂
c++·qt·策略模式
huanggang98212 小时前
在Ubuntu22.04上使用Qt Creator开发ROS2项目
qt·ros2
老秦包你会12 小时前
Qt第三课 ----------容器类控件
开发语言·qt
spygg14 小时前
Qt低版本多网卡组播bug
qt·组播·多网卡组播·qt5.7.0
码农客栈14 小时前
qt QWebSocketServer详解
qt
plmm烟酒僧15 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Black_Friend16 小时前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC16 小时前
【Qt】QTreeView 和 QStandardItemModel的关系
qt