如题:
mainwindow.ui
ui文件随便建立一个Qlabel控件就行

头文件
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();enum  Type {None          = 0,Amplification ,Shrink,Lift,Right,Up,Down,Move};private:Ui::MainWindow *ui;QPixmap  *pix;int action;          //动作(放大,缩小,移动...)int pixW;            //图片宽int pixH;            //图片高QRect Paint;         //绘画区域QLabel label;float ratio;                //比例QPoint offset;              //一次的图片偏移值QPoint Alloffset;           //总偏移void AddComboItem(QComboBox* cmbo);bool event(QEvent * event);void wheelEvent(QWheelEvent* e);     //鼠标滑轮事件
private slots:void paintEvent(QPaintEvent *event);
};#endif // MAINWINDOW_H

源文件:
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QApplication>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow),//Paint(10,10,810,810),Alloffset(0,0)
{ui->setupUi(this);ratio= 1.0;             //初始化图片缩放比例action = MainWindow::None;pixW = 985;            //设置图片尺寸为985*740pixH = 740;pix = new QPixmap;pix->load("D:/tu/test2.jpg");//   ui->label;}MainWindow::~MainWindow()
{delete ui;
}bool MainWindow::event(QEvent * event)
{static bool press=false;static QPoint PreDot;if(event->type() == QEvent::MouseButtonPress ){QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);//判断鼠标是否是左键按下,且鼠标位置是否在绘画区域if(mouse->button()==Qt::LeftButton &&ui->label->geometry().contains(mouse->pos())){press=true;QApplication::setOverrideCursor(Qt::OpenHandCursor); //设置鼠标样式PreDot = mouse->pos();}}
else if(event->type() == QEvent::MouseButtonRelease)
{QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);//判断鼠标是否是左键释放,且之前是在绘画区域if(mouse->button()==Qt::LeftButton && press ){QApplication::setOverrideCursor(Qt::ArrowCursor); //改回鼠标样式press=false;}
}if(event->type() == QEvent::MouseMove)              //移动图片{if(press){QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);offset.setX(mouse->x() - PreDot.x());offset.setY(mouse->y() - PreDot.y());PreDot = mouse->pos();action = MainWindow::Move;this->update();}}
return QWidget::event(event);
}void MainWindow::wheelEvent(QWheelEvent* event)     //鼠标滑轮事件
{if (event->delta()>0) {      //上滑,缩小action=MainWindow::Shrink;this->update();} else {                    //下滑,放大action=MainWindow::Amplification;this->update();}event->accept();
}void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);int NowW = ratio *pixW;int NowH = ratio *pixH;if(action==MainWindow::Amplification)           //缩小
{ratio-=0.1*ratio;if(ratio<0.18)ratio = 0.1;}
else  if(action==MainWindow::Shrink)           //放大
{ratio+=0.1*ratio;if(ratio>4.5)ratio = 5.000;}
if(action==MainWindow::Amplification || action==MainWindow::Shrink)      //更新图片
{NowW = ratio *pixW;NowH = ratio *pixH;pix->load("D:/tu/test2.jpg");                 //重新装载,因为之前的图片已经被缩放过*pix = pix->scaled(NowW, NowH,Qt::KeepAspectRatio);action=MainWindow::None;}if(action==MainWindow::Move)                    //移动
{int offsetx=Alloffset.x()+offset.x();Alloffset.setX(offsetx);int offsety=Alloffset.y()+offset.y();Alloffset.setY(offsety);action=MainWindow::None;
}if(abs(Alloffset.x())>=(ui->label->width()/2 + NowW/2 -10))    //限制X偏移值
{if(Alloffset.x()>0)Alloffset.setX(ui->label->width()/2 + NowW/2 -10);elseAlloffset.setX(-ui->label->width()/2 + -NowW/2 +10);
}
if(abs(Alloffset.y())>=(ui->label->height()/2 + NowH/2 -10))    //限制Y偏移值
{if(Alloffset.y()>0)Alloffset.setY(ui->label->height()/2 + NowH/2 -10);elseAlloffset.setY(-ui->label->height()/2 + -NowH/2 +10);}int x = ui->label->width()/2 + Alloffset.x() -NowW/2;
if(x<0)x=0;int y = ui->label->height()/2 + Alloffset.y() -NowH/2;
if(y<0)y=0;int  sx = NowW/2 - ui->label->width()/2 - Alloffset.x();
if(sx<0)sx=0;int  sy = NowH/2 - ui->label->height()/2 - Alloffset.y();
if(sy<0)sy=0;int w =(NowW - sx)>ui->label->width()? ui->label->width() : (NowW - sx);
if(w>(ui->label->width()-x))w = ui->label->width()-x;int h =(NowH - sy)>ui->label->height()? ui->label->height() : (NowH - sy);
if(h>(ui->label->height()-y))h = ui->label->height()-y;painter.drawRect(ui->label->x()-1,ui->label->y()-1,ui->label->width()+1,ui->label->height()+1); //画框painter.drawTiledPixmap(x+ui->label->x(),y+ui->label->y(),w,h,*pix,sx,sy);             //绘画图形
}

效果:


参考:https://www.cnblogs.com/lifexy/p/9057046.html
会出现无法加载大图片问题,建议使用QImage打开,然后在转换为QPixmap

QT QLabel显示图片并通过鼠标滑动改变大小以及移动图片相关推荐

  1. IDEA设置字体随鼠标滑动改变大小

    第一步:File--->Settings--->Editor--->General 第二步:在Mouse Control下勾选Change font size with+Mouse ...

  2. QT学习笔记(三)——vs2019+Qt实现打开影像并以鼠标为中心用滚轮控制图片缩放

    vs2019+Qt实现打开影像并以鼠标为中心用滚轮控制图片缩放 之前写了一个博客讲怎么显示一张影像,那个是基于Qpainter的 今天使用QLabel来显示影像,并且用鼠标滚轮控制缩放. 关于图像的打 ...

  3. 基于layer的图片弹出展示,默认原大小展示。图片太大则等比例缩小

    /**** html:<img src="img/1.jpg" onclick="showimg('img/1.jpg');">* 图片弹出展示,默 ...

  4. qt显示html富文本图片,Qt QLabel显示图片 动画 富文本

    0 前言动画 QLabel 功能不少, 能够用来显示图片 动画 富文本this 1 效果.net 对于图片和动画, 先添加到资源文件再使用3d 右键 Resources -> 添加现有文件cod ...

  5. Qt QLabel 显示gif动图

    #include <QMovie>QMovie * move = new QMovie(":/gif/牵着我的手去浪迹天涯.gif");ui->label_gif ...

  6. antd 能自适应吗_react 基于antd表格自适应宽度显示。。。鼠标滑动显示详细内容解决办法...

    最终效果内容如下 创建公共组件获取表格宽度 class EllipsisTooltip extends React.Component { constructor(props){ super(prop ...

  7. cocosCreator 精灵图片加载(一键还原大小+动态修改图片)

    问题:在cocosCreator中没找到对图片重置大小的按钮(在cocos2d-x中,图片可以通过重置大小来快速还原图片的原本大小) 所以在cocosCreator中替换资源的时候,图片资源和原来不同 ...

  8. html可变换的背景图片,HTML用ONMOUSEOVER,ONMOUSEOUT改变背景色或背景图片的方法_html/css_WEB-ITnose...

    1.用onmouseout onmouseover 图像间相互变换: 2.onmouseover onmouseout 改变文字背景色: οnmοuseοut="style.backgrou ...

  9. Qt实现 QLabel显示文本与图片、动图

    QLabel概念: 在Qt中,QLabel类用于文本或图片(包括动图gif).视频的显示,并不提供用户交互功能, 如果需要交互,比如响应鼠标事件,根据面向对象的里氏替换原则,子类必须能够替换它们的父类 ...

最新文章

  1. boot连接不上mysql数据库_【springboot连接 MYSQL数据库出问题_springboot】 | IT修真院·坑乎...
  2. 服务降级-启动时检查
  3. NUMA与英特尔下一代Xeon处理器学习心得(4)
  4. spring事务管理中,用try-catch处理了异常,事务也会回滚?
  5. SpringCloud与zuul
  6. 虚拟机登录/系统管理等命令
  7. 201604-1折点计数
  8. 计算机监控防误,计算机监控防误操作系统专利_专利申请于2007-10-25_专利查询 - 天眼查...
  9. java虚拟机之垃圾回收器
  10. 标准模型和IE模型的区别
  11. Linux 昨天时间
  12. PID控制算法的C语言实现十 模糊算法简介
  13. wallys/IPQ4019/IPQ4029/Access Point Wireless Module Dual band 11AC Wave2 Module
  14. 使用微软TTS语音引擎实现文本朗读
  15. OLED_I2C_SH1106屏幕教程
  16. 计算机学院运动会解说词,学院运动会入场解说词
  17. 北京博奥智源,发布ERP系统之财务管理及成本核算模块开发功能
  18. parameter缩略语_WB 术语及缩略语表
  19. T31项目架构选型方案
  20. jquery 动态添加div元素(两种方式)

热门文章

  1. 谷歌留痕霸屏平台有哪些?
  2. 北京ITSS认证流程
  3. Step1帐户登录系统
  4. android常问面试题及答案 经典
  5. IMU惯性测量单元学习与初始对准理论知识学习
  6. java简单模拟玩家
  7. 开发基于SpringBoot和BootStrap的全栈论坛网站(一):准备阶段
  8. 4284A安捷伦Agilent4284A精密lcr表
  9. Win10不能直接拖动文件打开的解决办法
  10. 华为 会议室分配时间最长_华为发布智慧办公战略产品 企业智慧屏系列