QLable提升类

实现:

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

1.myLabel.h 

#ifndef MYLABEL_H
#define MYLABEL_H

#include 

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

#include "myLabel.h"

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

}

你可能感兴趣的:(qt)