qt下怎么保持图片用滚轮缩放不失真啊,新手请大神指点
rt,现在想实现一个功能是缩放能在图片中心位置进行缩放,二是缩放不要失真,不知道为什么总是失真,新手求指导
代码:
#include "kk.h"
KK::KK(QWidget *parent)
: QWidget(parent,Qt::FramelessWindowHint)
{
pix.load( "picture.JPG",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
resize(pix.size());
// setMask(pix.mask());
}
void KK::mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
{
point=event->globalPos()-frameGeometry().topLeft();//globalPos()获取根窗口的相对路径,frameGeometry().topLeft获得主窗口左上角的位置
event->accept();
}
if(event->button()==Qt::RightButton)
{
close();
}
}
void KK::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons()==Qt::LeftButton)
{
move(event->globalPos()-point);
event->accept();
}
}
void KK::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,pix);//绘制图片到窗口
}
void KK::wheelEvent(QWheelEvent *event)
{
int numDegrees = event->delta()/8;
int numSteps=numDegrees/15;
qreal width = pix.width();
qreal height = pix.height();
pix=pix.scaled(width*(1+numSteps*0.15), height*(1+numSteps*0.15), Qt::KeepAspectRatio,Qt::SmoothTransformation);
resize(pix.size());
}
KK::~KK()
{
}