网页建站建设教程,电气工程师报考条件,网页设计接私单的网站,网页制作怎么做链接1、拖动区域放大
在 QCustomPlot 中实现 拖动区域放大#xff08;即通过鼠标左键拖动绘制矩形框选区域进行放大#xff09;的核心方法是设置 SelectionRectMode。具体操作步骤#xff1a;
1#xff09;禁用拖动模式 确保先关闭默认的图表拖动功能#xff08;否…1、拖动区域放大
在 QCustomPlot 中实现 拖动区域放大即通过鼠标左键拖动绘制矩形框选区域进行放大的核心方法是设置 SelectionRectMode。具体操作步骤
1禁用拖动模式 确保先关闭默认的图表拖动功能否则会冲突。
customPlot-setInteraction(QCP::iRangeDrag, false); // 关闭拖动
2启用框选放大模式 设置选择矩形模式为 srmZoom。
customPlot-setSelectionRectMode(QCP::SelectionRectMode::srmZoom); // 启用框选放大 3视觉效果定制可选 可自定义选框的边框和填充颜色。
customPlot-selectionRect()-setPen(QPen(Qt::black, 1, Qt::DashLine)); // 虚线边框
customPlot-selectionRect()-setBrush(QBrush(QColor(0,0,100,50))); // 半透明蓝色填充
注意拖拽与框选模式互斥 拖动 (iRangeDrag) 与框选放大 (srmZoom) 无法同时生效。若需切换功能如右键拖动、左键框选需自定义鼠标事件处理逻辑 。
2、 恢复原始视图
添加按钮或快捷键调用 rescaleAxes() 可一键重置坐标轴显示范围。或通过 setRange 手动重置坐标轴范围。
右键点击回到未放大状态撤销缩放操作的功能可以通过以下两种方式实现。
1方式一使用内置复位按钮推荐简单场景
添加复位按钮 创建按钮触发 rescaleAxes() 恢复初始视图 customPlot-rescaleAxes(); // 自动重置坐标轴范围customPlot-replot(); // 重绘图表
右键菜单集成复位选项 在右键菜单中添加复位选项
void MyCustomPlot::contextMenuEvent(QContextMenuEvent *event) {QMenu menu(this);QAction *resetAction menu.addAction(复位);connect(resetAction, QAction::triggered, this, MyCustomPlot::onResetZoom);menu.exec(event-globalPos());
}void MyCustomPlot::onResetZoom() {rescaleAxes();replot();
}2方式二缩放历史栈
适用于需要逐步撤销多次缩放操作的场景
2.1定义历史记录栈
QStackQPairQCPRange, QCPRange zoomHistory; // 存储(x轴范围, y轴范围)
2.2保存缩放前的状态 在缩放操作前保存当前坐标轴范围
void MyCustomPlot::beforeZoom() {zoomHistory.push(qMakePair(xAxis-range(), yAxis-range()));
}2.3右键触发撤销操作
void MyCustomPlot::mousePressEvent(QMouseEvent *event) {if (event-button() Qt::RightButton !zoomHistory.isEmpty()) {QPairQCPRange, QCPRange prevRange zoomHistory.pop();xAxis-setRange(prevRange.first); // 恢复x轴yAxis-setRange(prevRange.second); // 恢复y轴replot();}QCustomPlot::mousePressEvent(event);
}