实现该功能主要通过对QGraphicsView进行重现,才能实现鼠标控制对图片的缩放和拖动
使用方法:
1.添加重写后的文件
SOURCES += \
interactive_view.cpp \
pixitem.cpp
HEADERS += \
interactive_view.h \
pixitem.h
2.调用
QGraphics Scene *m_scene;
InteractiveView *view ; //对整个界面进行重写
PixItem *m_pixItem; //对item进行重写
QPixmap *pixmap;
view=new InteractiveView(ui->graphicsView); //重载重写之后graphicsView
m_scene=new QGraphicsScene; //声明场景
view->resize(701, 479);//点击空白的窗口,程序会崩
m_scene->clear();//清除场景
//恢复到原来的尺寸
view->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
QMatrix q;
q.setMatrix(1,view->matrix().m12(),view->matrix().m21(),1,view->matrix().dx(),view->matrix().dy());
view->setMatrix(q,false);
pixmap = new QPixmap(filePath);
m_pixItem = new PixItem(pixmap);//加载图片到单元
m_scene->addItem(m_pixItem);//在场景中添加单元
//设置场景的宽高和起点
view->setSceneRect(-pixmap->width()/2,-pixmap->height()/2,pixmap->width(),pixmap->height());
//在窗口中添加场景
view->setScene(m_scene);
//对窗口进行缩放,填写参数为宽高缩放比例
view->scale(701.0/pixmap->width(),479.0/pixmap->height());
//显示窗口
view->show();
2022-05-17 08:55:49
6KB
QT
1