Qt 顯示圖片 滑輪滾動縮放 widget
#pragma once
#include <QWidget>
#include "ui_ShowImgWidget.h"
#include "RDS_Image_Define.h"
#include <memory>
class ShowImgWidget : public QWidget
{
Q_OBJECT
public:
ShowImgWidget(QWidget *parent = Q_NULLPTR);
~ShowImgWidget();
void SetImgSrc(std::shared_ptr<RDS_Image> iImgSrc);
void SetImgSrc(const QImage& Src);
protected:
void ImgCounter(); //進行顯示圖片擷取
void RectAdjust(); //進行擷取矩形
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
//滾輪縮放
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
private:
bool b_ScaleLock,bTranLock;
QImage mImgSrc,mImgOnShow;
QBrush mBrush;
QRect imgRect;
double mScale;
QPoint mouseLastPos;
Ui::ShowImgWidget ui;
};
#include "ShowImgWidget.h"
ShowImgWidget::ShowImgWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
mScale = 1.0;
}
ShowImgWidget::~ShowImgWidget()
{
}
void ShowImgWidget::SetImgSrc(std::shared_ptr<RDS_Image> iImgSrc){
if (iImgSrc){
this->mImgSrc = iImgSrc->toQImage();
this->imgRect = QRect(0, 0, mImgSrc.width(), mImgSrc.height());
ImgCounter(); //進行視口匹配裁剪
}
return;
}
void ShowImgWidget::ImgCounter(){
this->mImgOnShow = mImgSrc.copy(this->imgRect).scaled(this->size(),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
return;
}
void ShowImgWidget::RectAdjust(){
this->imgRect.setX(qMax(0.0, qMin(qreal(this->imgRect.x()), mImgSrc.width() - mImgSrc.width() / this->mScale)));
this->imgRect.setY(qMax(0.0, qMin(qreal(this->imgRect.y()), mImgSrc.height() - mImgSrc.height() / this->mScale)));
this->imgRect.setWidth( mImgSrc.width() / this->mScale);
this->imgRect.setHeight(mImgSrc.height() / this->mScale);
return;
}
void ShowImgWidget::SetImgSrc(const QImage& Src){
this->mImgSrc = Src;
ImgCounter(); //進行視口匹配裁剪
}
#include "qpainter.h"
void ShowImgWidget::paintEvent(QPaintEvent *event) {
QPainter painterTemp(this);
painterTemp.setRenderHint(QPainter::Antialiasing, true);
painterTemp.setRenderHint(QPainter::SmoothPixmapTransform,true );
QPointF center(this->mImgOnShow.width() / qreal(2), this->mImgOnShow.height() / qreal(2));
//painterTemp.translate(ctrlPoints.at(0) - center);
//painterTemp.translate(center);
//painterTemp.translate(-center);
painterTemp.drawImage(QPointF(0, 0), this->mImgOnShow);
return;
}
void ShowImgWidget::resizeEvent(QResizeEvent *event){
RectAdjust();
ImgCounter();
return;
}
#include "qevent.h"
//滾輪縮放
void ShowImgWidget::wheelEvent(QWheelEvent *e){
mScale += e->delta() / qreal(1000);
//最大比例為圖片畫素與 視窗畫素 1:1
mScale = qMax(qreal(1), qMin(qreal(mImgSrc.width()/this->width()), mScale));
RectAdjust();
ImgCounter();
this->update();
return;
}
void ShowImgWidget:: mousePressEvent(QMouseEvent *event) {
qDebug("MP");
if (event->button() == Qt::LeftButton){ //進入位移模式
bTranLock = true;
mouseLastPos = event->pos();
}
else if (event->button() == Qt::RightButton){ //復位
mouseLastPos = QPoint(0, 0);
mScale = 1;
bTranLock = false;
this->imgRect = QRect(0, 0, mImgSrc.width() / this->mScale, mImgSrc.height() / this->mScale);
RectAdjust();
ImgCounter();
}
return;
}
void ShowImgWidget::mouseReleaseEvent(QMouseEvent *event){
qDebug("MR");
bTranLock = false;
RectAdjust();
ImgCounter();
this->update();
return;
}
void ShowImgWidget::mouseMoveEvent(QMouseEvent *event){
if (this->bTranLock){ //如果在位移模式之中
auto PP = this->mouseLastPos - event->pos();
imgRect.setX(imgRect.x() + PP.x());
imgRect.setY(imgRect.y() + PP.y());
this->imgRect = QRect(imgRect.x(), imgRect.y(),
mImgSrc.width() / this->mScale, mImgSrc.height() / this->mScale);
RectAdjust();
ImgCounter();
this->update();
this->mouseLastPos = event->pos();
}
else {
}
return;
}