QGgraphicsView鼠标缩放

// 20260615 鼠标缩放

void WorkInfoForm::wheelEvent(QWheelEvent* event) {

// 获取当前的鼠标所在的view坐标;

QPointF prev_viewPos = event->position();

// 获取当前鼠标相对于scene的位置;

QPointF prev_scenePos = ui->teach_graphicsView->mapToScene(prev_viewPos.toPoint());

//...计算缩放比例

// 2. 计算缩放因子

double scaleFactor = 1.15;

// 2. 定义最小和最大缩放比例

double currentScale = ui->teach_graphicsView->transform().m11();

复制代码
const double minScale = 1;  // 最小缩小到 10%
const double maxScale = 5.0;  // 最大放大到 500%

//double scaleFactor = 1.15;
bool willZoomIn = (event->angleDelta().y() > 0);

// 3. 计算缩放后的预期比例
double nextScale = willZoomIn ? (currentScale * scaleFactor) : (currentScale / scaleFactor);

// 4. 【关键】如果超出范围,则不执行缩放
if (willZoomIn && nextScale > maxScale) {
    return; // 超过最大限制,忽略本次操作
}
if (!willZoomIn && nextScale < minScale) {
    return; // 低于最小限制,忽略本次操作
}

if (willZoomIn) {
    ui->teach_graphicsView->scale(scaleFactor, scaleFactor);
}
else {
    ui->teach_graphicsView->scale(1 / scaleFactor, 1 / scaleFactor);
}
qDebug() << "缩放比:" << ui->teach_graphicsView->transform().m11();
//ui->teach_graphicsView->scale(scaleTemp, scaleTemp);  //缩放

//20260615 有问题 ui->teach_graphicsView->setSceneRect(ui->teach_graphicsView->mapToScene(this->rect()).boundingRect()); //调整scene,使得scene和view一直,主要是为了排除掉scroll

复制代码
//获取缩放后的scene坐标
QPointF scenePos = ui->teach_graphicsView->mapToScene(prev_viewPos.toPoint());
//获取缩放前后的坐标差值,即为需要进行move的位移
QPointF disPointF = scenePos - prev_scenePos;
//    qDebug()<<prev_scenePos<<" ::: "<<scenePos<<disPointF;
qDebug() << ui->teach_graphicsView->scene()->sceneRect();
//QRectF sceneRect = ui->teach_graphicsView->scene()->sceneRect();
//if (sceneRect.width() > 10000 || sceneRect.height() > 10000) {
//    sceneRect = sceneRect.normalized().adjusted(-5000, -5000, 5000, 5000);
//    ui->teach_graphicsView->scene()->setSceneRect(sceneRect);
//}
//ui->teach_graphicsView->horizontalScrollBar()->setValue(ui->teach_graphicsView->horizontalScrollBar()->value() + disPointF.x());
//ui->teach_graphicsView->verticalScrollBar()->setValue(ui->teach_graphicsView->verticalScrollBar()->value() + disPointF.y());
////调整位置
//ui->teach_graphicsView->scene()->setSceneRect(ui->teach_graphicsView->scene()->sceneRect().x() - disPointF.x(), ui->teach_graphicsView->scene()->sceneRect().y() - disPointF.y(),
// ui->teach_graphicsView->scene()->sceneRect().width(), ui->teach_graphicsView->scene()->sceneRect().height());
//    emit signal_wheel(m_scale);
ui->teach_graphicsView->scene()->update();

}

相关推荐
架构师伯子明16 小时前
QT+OpenGL(3):FBO离屏渲染
开发语言·qt
mengzhi啊19 小时前
把数据存入sql数据库。qt,c++。
数据库·sql·qt
mengzhi啊1 天前
SerialPortItem对串口再封装一层,省去一些常用的步骤,不用每次都去写类
qt
用户8334423966142 天前
从批量操作到任务中心,再到现场工作台:DeviceForge v2.10—v2.12 完整演进
qt
宛 禾2 天前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习
打工仔折腾 AI2 天前
Qt网络编程实战:UDP、TCP到HTTP的落地经验与选型
网络·人工智能·后端·python·qt·udp·ai agent 实战
Lhan.zzZ2 天前
Linux + Qt/QML:解决 QtQuick.VirtualKeyboard.Plugins 模块加载失败与环境变量配置
linux·数据库·qt
誰能久伴不乏2 天前
QT工业上位机一次「指令静默丢失」的排查
qt·docker·github
扶尔魔ocy3 天前
【qt openGL入门笔记】OSG实现飞机模型在网格图上飞行并进行态势标绘(四)
qt·opengl·osg
橙橙笔记3 天前
QT安卓软件的制作
android·git·qt·软件