// 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();
}