头文件:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication.h"
class QtGuiApplication : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication(QWidget *parent = Q_NULLPTR);
void paintEvent(QPaintEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void InitShowPix();
private:
Ui::QtGuiApplicationClass ui;
QRect paintRect;
QRect pixRect;
QImage *image;
QPixmap *pixmap;
QPoint pixOffset;
QPoint lastCursorPos;
QSize initPixSize;
QSize initShowPixSize;
QSize currentShowPixSize;
bool b_LButtonPressed;
int maxMultiple;
};
源文件:
#include "QtGuiApplication.h"
#include<qpainter.h>
#include<QMouseEvent>
#include<qmessagebox.h>
#pragma execution_character_set("utf-8");
QtGuiApplication::QtGuiApplication(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
paintRect = QRect(10, 10, 510, 310);//宽度和高度最好为偶数
b_LButtonPressed = false;
maxMultiple = 3;//最大的放大倍数
image = new QImage("house.jpg");
initPixSize = image->size();
pixmap = new QPixmap();
InitShowPix();
initShowPixSize = pixmap->size();
}
void QtGuiApplication::InitShowPix()
{
*pixmap = QPixmap::fromImage(*image);
if (pixmap->width() > paintRect.width() || pixmap->height() > paintRect.height())
*pixmap = pixmap->scaled(paintRect.width(), paintRect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
currentShowPixSize = pixmap->size();
pixOffset = QPoint(0, 0);
}
void QtGuiApplication::paintEvent(QPaintEvent *event)
{
QPainter *painter = new QPainter(this);
painter->drawRect(paintRect);
/*超出的部分不显示*/
if (currentShowPixSize.width() > paintRect.width())//限制X偏移值
{
int offsetX = currentShowPixSize.width() / 2.0f - paintRect.width() / 2.0f + 20;
if (pixOffset.x() > offsetX)
pixOffset.setX(offsetX);
if (pixOffset.x() < -offsetX)
pixOffset.setX(-offsetX);
}
if (currentShowPixSize.height() > paintRect.height())//限制Y偏移值
{
int offsetY = currentShowPixSize.height() / 2.0f - paintRect.height() / 2.0f + 20;
if (pixOffset.y() > offsetY)
pixOffset.setY(offsetY);
if (pixOffset.y() < -offsetY)
pixOffset.setY(-offsetY);
}
int x = paintRect.width() / 2.0f + pixOffset.x() - currentShowPixSize.width() / 2.0f;
if (x < 0)
x = 0;
int y = paintRect.height() / 2.0f + pixOffset.y() - currentShowPixSize.height() / 2.0f;
if (y < 0)
y = 0;
int sx = currentShowPixSize.width() / 2.0f - paintRect.width() / 2.0f - pixOffset.x();
if (sx < 0)
sx = 0;
int sy = currentShowPixSize.height() / 2.0f - paintRect.height() / 2.0f - pixOffset.y();
if (sy < 0)
sy = 0;
int w = (currentShowPixSize.width() - sx) > paintRect.width() ? paintRect.width() : (currentShowPixSize.width() - sx);
if (w > (paintRect.width() - x))
w = paintRect.width() - x;
int h = (currentShowPixSize.height() - sy) > paintRect.height() ? paintRect.height() : (currentShowPixSize.height() - sy);
if (h > (paintRect.height() - y))
h = paintRect.height() - y;
pixRect = QRect(x + paintRect.x() + 1, y + paintRect.y() + 1, w - 1, h - 1);
painter->drawTiledPixmap(pixRect, *pixmap, QPoint(sx, sy));
}
void QtGuiApplication::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton&&
paintRect.contains(event->pos()))
{
if (!currentShowPixSize.isEmpty())
{
if (currentShowPixSize.width() > initShowPixSize.width() ||
currentShowPixSize.height() > initShowPixSize.height())
{
InitShowPix();
this->update();
}
else if (currentShowPixSize != initPixSize)
{
*pixmap = QPixmap::fromImage(*image);
currentShowPixSize = pixmap->size();
this->update();
}
}
}
}
void QtGuiApplication::mousePressEvent(QMouseEvent *event)
{
if (pixRect.contains(event->pos()) && event->button() == Qt::LeftButton)
{
b_LButtonPressed = true;
lastCursorPos = event->pos();
}
}
void QtGuiApplication::mouseMoveEvent(QMouseEvent *event)
{
if (b_LButtonPressed)
{
if (paintRect.contains(event->pos()))
{
if (currentShowPixSize.width() > paintRect.width())
{
pixOffset.setX(pixOffset.x() + (event->pos().x() - lastCursorPos.x()));
this->update();
lastCursorPos.setX(event->pos().x());
}
if (currentShowPixSize.height() > paintRect.height())
{
pixOffset.setY(pixOffset.y() + (event->pos().y() - lastCursorPos.y()));
this->update();
lastCursorPos.setY(event->pos().y());
}
}
else
b_LButtonPressed = false;
}
}
void QtGuiApplication::mouseReleaseEvent(QMouseEvent *event)
{
b_LButtonPressed = false;
}
void QtGuiApplication::wheelEvent(QWheelEvent *event)
{
if (paintRect.contains(event->pos()))
{
if (event->delta() > 0)//上滑,放大
{
if (currentShowPixSize.width() < initPixSize.width() * maxMultiple)
{
*pixmap = QPixmap::fromImage(*image);
*pixmap = pixmap->scaledToWidth(currentShowPixSize.width()*1.2f, Qt::SmoothTransformation);
if (pixmap->width() > initPixSize.width() * maxMultiple)
{
*pixmap = QPixmap::fromImage(*image);
*pixmap = pixmap->scaledToWidth(initPixSize.width() * maxMultiple, Qt::SmoothTransformation);
}
currentShowPixSize = pixmap->size();
this->update();
}
}
else//下滑,缩小
{
if (currentShowPixSize.width() > initShowPixSize.width() ||
currentShowPixSize.height() > initShowPixSize.height())
{
*pixmap = QPixmap::fromImage(*image);
*pixmap = pixmap->scaledToWidth(currentShowPixSize.width()*0.8f, Qt::SmoothTransformation);
currentShowPixSize = pixmap->size();
if (currentShowPixSize.width() < initShowPixSize.width() ||
currentShowPixSize.height() < initShowPixSize.height())
InitShowPix();
else if (currentShowPixSize == initShowPixSize)
pixOffset = QPoint(0, 0);
if (currentShowPixSize.width() <= paintRect.width())
pixOffset.setX(0);
if (currentShowPixSize.height() <= paintRect.height())
pixOffset.setY(0);
this->update();
}
}
}
}