ZIPQt 图片交互-鼠标滚轮放缩、拖拽、选点 7.55KB

qq_29912325

资源文件列表:

Qt图片交互.zip 大约有13个文件
  1. .gitignore 814B
  2. HI_ImageBox.pro 996B
  3. HI_ImageBox_Demo/
  4. HI_ImageBox_Demo/.gitignore 814B
  5. HI_ImageBox_Demo/HI_ImageBox_Demo.pro 1.29KB
  6. HI_ImageBox_Demo/main.cpp 183B
  7. HI_ImageBox_Demo/mainwindow.cpp 765B
  8. HI_ImageBox_Demo/mainwindow.h 457B
  9. HI_ImageBox_Demo/mainwindow.ui 1.23KB
  10. hi_graphicsview.cpp 3.08KB
  11. hi_graphicsview.h 1.04KB
  12. hi_imagebox.cpp 804B
  13. hi_imagebox.h 1.56KB

资源介绍:

Qt 图片交互——鼠标滚轮放缩、拖拽、选点(工程源代码,编写的库方便使用,包含demo)演示视频:https://www.bilibili.com/video/BV1yv411P7hJ/
#include "hi_graphicsview.h" #include <QGraphicsPixmapItem> #include "hi_imagebox.h" HI_GraphicsView::HI_GraphicsView(QWidget *parent): QGraphicsView(parent), m_isTranslate(false), m_scene(new QGraphicsScene()), m_imageItem(new QGraphicsPixmapItem()) { m_scene->addItem(m_imageItem); setScene(m_scene); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setRenderHint(QPainter::Antialiasing); setSceneRect(INT_MIN/2, INT_MIN/2, INT_MAX, INT_MAX); setTransformationAnchor(QGraphicsView::AnchorViewCenter); centerOn(0, 0); } HI_GraphicsView::~HI_GraphicsView() { m_scene->deleteLater(); delete m_imageItem; } void HI_GraphicsView::SetImage(const QImage &image) { m_imageItem->setPixmap(QPixmap::fromImage(image)); QPoint newCenter(image.width() / 2 ,image.height()/2); //设置scene中心到图像中点 centerOn(newCenter); show(); } void HI_GraphicsView::wheelEvent(QWheelEvent *event) { // 滚轮的滚动量 QPoint scrollAmount = event->angleDelta(); // 正值表示滚轮远离使用者放大负值表示朝向使用者缩小 scrollAmount.y() > 0 ? ZoomIn() : ZoomOut(); } void HI_GraphicsView::mouseMoveEvent(QMouseEvent *event) { if(m_isTranslate) { //获取 QPointF mouseDelta = event->pos()-m_lastMousePos; Translate(mouseDelta); } m_lastMousePos = event->pos(); QGraphicsView::mouseMoveEvent(event); } void HI_GraphicsView::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { m_isTranslate = true; m_lastMousePos = event->pos(); } else if(event->button()==Qt::RightButton) { QPointF point = mapToScene(event->pos()); //只有点击图片时才发送 if (scene()->itemAt(point, transform()) != NULL) { emit m_imageBox->ImageClick(point.x(),point.y()); } } QGraphicsView::mousePressEvent(event); } void HI_GraphicsView::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) m_isTranslate = false; QGraphicsView::mouseReleaseEvent(event); } void HI_GraphicsView::mouseDoubleClickEvent(QMouseEvent *event) { centerOn(m_imageItem->pixmap().width()/2,m_imageItem->pixmap().height()/2); QGraphicsView::mouseDoubleClickEvent(event); } void HI_GraphicsView::ZoomIn() { Zoom(1.1); } void HI_GraphicsView::ZoomOut() { Zoom(0.9); } void HI_GraphicsView::Zoom(float scaleFactor) { // 防止过小或过大 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); if (factor < 0.07 || factor > 100) return; scale(scaleFactor, scaleFactor); } void HI_GraphicsView::Translate(QPointF delta) { int w = viewport()->rect().width(); int h = viewport()->rect().height(); QPoint newCenter(w / 2. - delta.x()+0.5, h / 2. - delta.y()+0.5); centerOn(mapToScene(newCenter)); }
100+评论
captcha
    类型标题大小时间
    ZIPwordpress主题 仿Hao123 网址导航主题58.09KB12年前
    ZIPGigaDevice_MCU_ISP_Programmer_V3.0.2.5782.zip1.25MB3年前
    ZIP基于51单片机的智能电子秤系统设计(含代码仿真及论文)18.73MB1年前
    ZIP计算机网络自顶向下方法第八版答案14.56MB1年前
    ZIP自己写的stm32f103的ov7670摄像头程序4.28MB11年前
    ZIPTomcat 6.0软件下载6.18MB16年前
    ZIP雷电模拟器9改真机环境.zip31.45MB1年前
    ZIPflex的组织结构图组件207.43KB17年前