由于工作需要需要编写一个图片查看器,需要支持鼠标滚动缩放大小,鼠标移动图片位置等,由于无法上传gif,截取的gif示例图暂时不上传了,讲一下大概思路
1 鼠标点击移动图片
通过重写鼠标点击事件获取到点位,在鼠标移动事件中进行move操作,在paintEvent中指定绘制起始点即可, 与QDialog 移动操作一样,代码如下
//mousePressEvent
void PicShowDlg::mousePressEvent(QMouseEvent *event)
{
QDialog::mousePressEvent(event);
if (this->rect().contains(event->pos()))
{
QPoint p(m_pix.rect().x(),m_pix.rect().y());
QSize size = m_pix.size();
m_dPixTempPos = event->globalPos() - m_dPixPos;
}
}
//鼠标移动事件
void PicShowDlg::mouseMoveEvent(QMouseEvent *event)
{
QDialog::mouseMoveEvent(event);
if (this->rect().contains(event->pos()))
{
m_dPixPos = event->globalPos() - m_dPixTempPos;
}
}
2 放大缩小图片
通过重写滚轮事件,通过滚动设置比例系数,在paintEvent中重新计算缩放后的图片大小
//鼠标滚轮事件
void PicShowDlg::wheelEvent(QWheelEvent *event)
{
if (event->delta() > 0)
{
m_rate -= 0.1;
if (m_rate < 0.018)
m_rate = 0.01;
}
else if (event->delta() < 0 )
{
m_rate += 0.1;
if (m_rate >4.5)
{
m_rate = 5;
}
}
update();
return QDialog::wheelEvent(event);
}
// paintEvent 事件重新
void PicShowDlg::paintEvent(QPaintEvent *event)
{
QDialog::paintEvent(event);
QPainter p(this);
int nH = m_pix.height()*m_rate;
int nW = m_pix.width()*m_rate;
QPixmap newPix = m_pix.scaled(nW,nH,Qt::KeepAspectRatio,Qt::SmoothTransformation);
p.drawPixmap(QRect(m_dPixPos.rx()-(nW - m_pix.width())/2,m_dPixPos.ry()-(nH - m_pix.height())/2, newPix.width(),newPix.height()), newPix);
update();
}
picShowDlg类完整代码
.h文件
#pragma once
#include <QDialog>
#include <QPixmap>
#include <QPoint>
#include "ui_PicShowDlg.h"
class PicShowDlg:public QDialog
{
public:
explicit PicShowDlg(QWidget *parent = nullptr);
~PicShowDlg();
void SetPix(const QPixmap& pix );
void SetPix(const QString &strFile);
protected:
virtual void paintEvent(QPaintEvent *event) override;
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void wheelEvent(QWheelEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override;
private:
Ui::PicShowDlg *ui;
QPixmap m_pix;
//QRect m_paintRect;
QPoint m_dPixTempPos;
QPoint m_dPixPos;
double m_rate;
};
.cpp文件
#include <QPainter>
#include <QMouseEvent>
#include <QDesktopWidget>
#include "PicShowDlg.h"
PicShowDlg::PicShowDlg(QWidget *parent)
:QDialog(parent)
,ui(nullptr)
, m_rate(1)
{
ui = new Ui::PicShowDlg;
ui->setupUi(this);
this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
this->setModal(false);
this->show();
}
PicShowDlg::~PicShowDlg()
{
if(ui != nullptr)
{
delete ui;
ui = nullptr;
}
}
void PicShowDlg::SetPix(const QPixmap& pix)
{
m_pix = pix;
this->resize(m_pix.size());
}
void PicShowDlg::SetPix(const QString &strFile)
{
m_pix.load(strFile);
this->setWindowTitle(strFile);
if (m_pix.height() > QApplication::desktop()->height() -300)
{
this->resize(m_pix.size() + QSize(300,0));
}
else if(m_pix.width() > QApplication::desktop()->width() - 400)
{
this->resize(m_pix.size() + QSize(0, 200));
}
else
{
this->resize(m_pix.size() + QSize(300, 200));
}
}
void PicShowDlg::paintEvent(QPaintEvent *event)
{
QDialog::paintEvent(event);
QPainter p(this);
int nH = m_pix.height()*m_rate;
int nW = m_pix.width()*m_rate;
QPixmap newPix = m_pix.scaled(nW,nH,Qt::KeepAspectRatio,Qt::SmoothTransformation);
p.drawPixmap(QRect(m_dPixPos.rx()-(nW - m_pix.width())/2,m_dPixPos.ry()-(nH - m_pix.height())/2, newPix.width(),newPix.height()), newPix);
update();
}
void PicShowDlg::mousePressEvent(QMouseEvent *event)
{
QDialog::mousePressEvent(event);
if (this->rect().contains(event->pos()))
{
QPoint p(m_pix.rect().x(),m_pix.rect().y());
QSize size = m_pix.size();
m_dPixTempPos = event->globalPos() - m_dPixPos;
}
}
void PicShowDlg::mouseMoveEvent(QMouseEvent *event)
{
QDialog::mouseMoveEvent(event);
if (this->rect().contains(event->pos()))
{
m_dPixPos = event->globalPos() - m_dPixTempPos;
}
}
void PicShowDlg::wheelEvent(QWheelEvent *event)
{
if (event->delta() > 0)
{
m_rate -= 0.1;
if (m_rate < 0.018)
m_rate = 0.01;
}
else if (event->delta() < 0 )
{
m_rate += 0.1;
if (m_rate >4.5)
{
m_rate = 5;
}
}
update();
return QDialog::wheelEvent(event);
}
void PicShowDlg::resizeEvent(QResizeEvent *event)
{
this->move((QApplication::desktop()->width() - event->size().width())/2,(QApplication::desktop()->height() - event->size().height())/2);
m_dPixPos = QPoint((event->size().width() - m_pix.width())/2,(event->size().height() - m_pix.height())/2);
update();
}
工程代码链接