qt鼠标悬停图片放大_20.QT-Qpixmap实现图片鼠标缩放,鼠标拖动示例(详解)

#include "widget.h"Widget::Widget():

Paint(10,10,810,810),

BigButton("放大",this),

LittleButton("缩小",this),

LiftButton("向左",this),

RightButton("向右",this),

UpButton("向上",this),

DownButton("向下",this),

ResetButton("还原",this),

OpenButton("打开文件",this),

Alloffset(0,0),

label("100%",this)

{

ratio= 1.0; //初始化图片缩放比例

action =Widget::None;

BigButton.setGeometry(822,10,60,25);

connect(&BigButton,SIGNAL(clicked()),this,SLOT(onBigClicked()));

LittleButton.setGeometry(822,40,60,25);

connect(&LittleButton,SIGNAL(clicked()),this,SLOT(onLittleClicked()));

LiftButton.setGeometry(822,70,60,25);

connect(&LiftButton,SIGNAL(clicked()),this,SLOT(OnLiftClicked()));

RightButton.setGeometry(822,100,60,25);

connect(&RightButton,SIGNAL(clicked()),this,SLOT(OnRightClicked()));

UpButton.setGeometry(822,130,60,25);

connect(&UpButton,SIGNAL(clicked()),this,SLOT(onUpClicked()));

DownButton.setGeometry(822,160,60,25);

connect(&DownButton,SIGNAL(clicked()),this,SLOT(onDownClicked()));

ResetButton.setGeometry(822,190,60,25);

connect(&ResetButton,SIGNAL(clicked()),this,SLOT(onResetClicked()));

OpenButton.setGeometry(822,220,60,25);

connect(&OpenButton,SIGNAL(clicked()),this,SLOT(onOpenClicked()));

label.move(840,260);

resize(890,850);this->setWindowTitle("图片浏览器(请打开文件)");

}bool Widget::event(QEvent * event)

{static bool press=false;staticQPoint PreDot;if(event->type() ==QEvent::MouseButtonPress )

{

QMouseEvent*mouse = dynamic_cast(event);//判断鼠标是否是左键按下,且鼠标位置是否在绘画区域

if(mouse->button()==Qt::LeftButton &&Paint.contains(mouse->pos()))

{

press=true;

QApplication::setOverrideCursor(Qt::OpenHandCursor);//设置鼠标样式

PreDot= mouse->pos();

}

}else if(event->type() ==QEvent::MouseButtonRelease)

{

QMouseEvent*mouse = dynamic_cast(event);//判断鼠标是否是左键释放,且之前是在绘画区域

if(mouse->button()==Qt::LeftButton &&press )

{

QApplication::setOverrideCursor(Qt::ArrowCursor);//改回鼠标样式

press=false;

}

}if(event->type() == QEvent::MouseMove) //移动图片

{if(press)

{

QMouseEvent*mouse = dynamic_cast(event);

offset.setX(mouse->x() -PreDot.x());

offset.setY(mouse->y() -PreDot.y());

PreDot= mouse->pos();

action=Widget::Move;this->update();

}

}return QWidget::event(event);

}void Widget::wheelEvent(QWheelEvent* event) //鼠标滑轮事件

{if (event->delta()>0) { //上滑,缩小

action=Widget::Shrink;this->update();

}else { //下滑,放大

action=Widget::Amplification;this->update();

}event->accept();

}void Widget::paintEvent(QPaintEvent *event)

{

QPainter painter(this);

painter.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing|QPainter::TextAntialiasing);

painter.drawRect(Paint.x()-1,Paint.y()-1,Paint.width()+1,Paint.height()+1); //画框

if(image.isNull())

{return;

}int NowW = ratio *pixW;int NowH = ratio *pixH;if(action==Widget::Shrink) //缩小

{

ratio-=0.05*ratio;if(ratio<0.018)

ratio= 0.01;/*显示比例*/QString str;

str.sprintf("%.0f%",ratio*100);

label.setText(str) ;

qDebug()<

}else if(action==Widget::Amplification) //放大

{

ratio+=0.05*ratio;if(ratio>4.5)

ratio= 5.000;/*显示比例*/QString str;

str.sprintf("%.0f%",ratio*100);

label.setText(str);

qDebug()<

}if(action==Widget::Amplification || action==Widget::Shrink || action==Widget::Reset) //更新图片

{

NowW= ratio *pixW;

NowH= ratio *pixH;

crtPix= pix.scaled(NowW, NowH,Qt::KeepAspectRatio,Qt::SmoothTransformation); //重新装载

action=Widget::None;

}if(action==Widget::Move) //移动

{int offsetx=Alloffset.x()+offset.x();

Alloffset.setX(offsetx);int offsety=Alloffset.y()+offset.y();

Alloffset.setY(offsety);

action=Widget::None;

}if(abs(Alloffset.x())>=(Paint.width()/2 + NowW/2 -10)) //限制X偏移值

{if(Alloffset.x()>0)

Alloffset.setX(Paint.width()/2 + NowW/2 -10);elseAlloffset.setX(-Paint.width()/2 + -NowW/2 +10);

}if(abs(Alloffset.y())>=(Paint.height()/2 + NowH/2 -10)) //限制Y偏移值

{if(Alloffset.y()>0)

Alloffset.setY(Paint.height()/2 + NowH/2 -10);elseAlloffset.setY(-Paint.height()/2 + -NowH/2 +10);

}int x = Paint.width()/2 + Alloffset.x() -NowW/2;if(x<0)

x=0;int y = Paint.height()/2 + Alloffset.y() -NowH/2;if(y<0)

y=0;int sx = NowW/2 - Paint.width()/2 -Alloffset.x();if(sx<0)

sx=0;int sy = NowH/2 - Paint.height()/2 -Alloffset.y();if(sy<0)

sy=0;int w =(NowW - sx)>Paint.width()? Paint.width() : (NowW -sx);if(w>(Paint.width()-x))

w= Paint.width()-x;int h =(NowH - sy)>Paint.height()? Paint.height() : (NowH -sy);if(h>(Paint.height()-y))

h= Paint.height()-y;

painter.drawTiledPixmap(x+Paint.x(),y+Paint.y(),w,h,crtPix,sx,sy); //绘画图形

}voidWidget::onLittleClicked()

{

action=Widget::Amplification;this->update();

}voidWidget::onOpenClicked()

{

QString str= QFileDialog::getOpenFileName(this,"open","D:","img (*.png *.jpg)");if(!str.isNull())

{

image.load(str);

pix=pix.fromImage(image);

crtPix=pix;

pixW= image.width(); //图片宽

pixH = image.height(); //图片高

qDebug()<setWindowTitle("图片浏览器("+str+")");

onResetClicked();

}

}voidWidget::onBigClicked()

{

action=Widget::Shrink;this->update();

}voidWidget::onUpClicked()

{

action=Widget::Move;

offset.setX(0);

offset.setY(-20);this->update();

}voidWidget::onDownClicked()

{

action=Widget::Move;

offset.setX(0);

offset.setY(20);this->update();

}voidWidget::onResetClicked()

{

action=Widget::Reset;

Alloffset.setX(0);

Alloffset.setY(0);

ratio= 1.000;

label.setText("100%");this->update();

}voidWidget::OnLiftClicked()

{

action=Widget::Move;

offset.setX(-20);

offset.setY(0);this->update();

}voidWidget::OnRightClicked()

{

action=Widget::Move;

offset.setX(20) ;

offset.setY(0) ;this->update();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值