公司动态
【QCustomPlot12】示例2:自定义选择框的样式
【QCustomPlot12】示例2自定义选择框的样式一、函数原型1. srmNone无模式2. srmZoom缩放模式★最常用3. srmSelect选择模式4. srmCustom自定义模式二、自定义选择矩形的行为方案一使用srmCustom方案二、使用自定义QCPItemRect原创作者郑同学的笔记原文链接https://zhengjunxue.blog.csdn.net/article/details/155307044QCustomPlot::setSelectionRectMode() 是 QCustomPlot 图表库中用于设置选择矩形模式的重要函数。选择矩形允许用户通过鼠标拖动在图表上选择一个区域用于数据选择、缩放等交互操作。一、函数原型void QCustomPlot::setSelectionRectMode(QCP::SelectionRectMode mode);SelectionRectMode 枚举值详解1. srmNone无模式作用禁用选择矩形功能行为所有鼠标事件都会传递给底层对象通常用于允许其他交互如坐标轴范围拖动典型应用customPlot-setSelectionRectMode(QCP::srmNone);// 此时可以通过鼠标拖动坐标轴2. srmZoom缩放模式★最常用作用通过选择矩形进行视图缩放行为拖拽鼠标绘制矩形释放后自动缩放缩放只作用于通过 setRangeZoomAxes() 设置的坐标轴示例// 设置为缩放模式customPlot-setSelectionRectMode(QCP::srmZoom);// 指定可缩放的坐标轴通常设置X和Y轴customPlot-axisRect()-setRangeZoomAxes(customPlot-xAxis,// X轴可缩放customPlot-yAxis// Y轴可缩放);3. srmSelect选择模式作用选择数据点行为拖拽绘制选择矩形释放后选择矩形内的数据点选择行为受 plottable 对象的可选择性设置控制数据选择机制需要数据点支持选择setSelectable()可单选或多选示例customPlot-setSelectionRectMode(QCP::srmSelect);// 设置图形可被选择graph-setSelectable(QCP::stSingleData);// 单选数据点// 或graph-setSelectable(QCP::stMultipleDataRanges);// 多选数据范围4. srmCustom自定义模式作用完全自定义选择矩形的行为行为绘制选择矩形但不执行任何内置操作程序员需要手动连接信号处理用户交互常用信号// 选择矩形被接受释放鼠标voidQCPSelectionRect::accepted(constQRectrect,QMouseEvent*event);// 选择矩形正在改变voidQCPSelectionRect::changed(constQRectrect,QMouseEvent*event);// 选择矩形被取消voidQCPSelectionRect::canceled(constQRectrect,QMouseEvent*event);二、自定义选择矩形的行为要求鼠标滑动时显示有颜色的矩形框鼠标松开时不放大也不移动颜色矩形框不消失。下次鼠标点击矩形框重新绘制方案一使用srmCustom#includemainwindow.h#includeui_mainwindow.h#includeQVBoxLayout#includeQMouseEvent#includeQDebug#includecmathMainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow),isDragging(false){ui-setupUi(this);// 创建布局和绘图区域QVBoxLayout*layoutnewQVBoxLayout(ui-centralWidget);customPlotnewQCustomPlot(this);layout-addWidget(customPlot);// 获取选择矩形对象并设置样式QCPSelectionRect*selectionRectcustomPlot-selectionRect();selectionRect-setPen(QPen(QColor(255,0,0),1,Qt::DashLine));// 设置边框颜色为红色虚线selectionRect-setBrush(QBrush(QColor(255,192,203,100)));// 设置填充颜色为粉色透明度100// // 0. 设置为禁用选择矩形功能// customPlot-setSelectionRectMode(QCP::srmNone);// customPlot-setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);// // 1. 设置为缩放模式最常见// customPlot-setSelectionRectMode(QCP::srmZoom);// customPlot-setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);// // 2. 设置为选择模式// customPlot-setSelectionRectMode(QCP::srmSelect);// customPlot-setInteractions(QCP::iSelectPlottables);// 3. 设置为自定义模式customPlot-setSelectionRectMode(QCP::srmCustom);connect(customPlot-selectionRect(),QCPSelectionRect::accepted,[](constQRectrect,QMouseEvent*){qDebug()Custom selection fromrect.topLeft()torect.bottomRight();});// 准备数据QVectordoublex(100),y(100);for(inti0;i100;i){x[i]i/10.0;// 0 到 10步长 0.1y[i]sin(x[i]);// 正弦波数据}// 创建图表customPlot-addGraph();customPlot-graph(0)-setData(x,y);// 配置坐标轴customPlot-xAxis-setLabel(X Axis);customPlot-yAxis-setLabel(Y Axis);customPlot-xAxis-setRange(0,10);customPlot-yAxis-setRange(-1.5,1.5);// 启用网格customPlot-xAxis-grid()-setVisible(true);customPlot-yAxis-grid()-setVisible(true);// 重绘图表customPlot-replot();}MainWindow::~MainWindow(){deleteui;}方案二、使用自定义QCPItemRect第一步自定义QCPItemRect第二步设置QCPItemRect的颜色样式并提升到顶层。第三步鼠标按下、移动、松开时定义QCPItemRect的状态和大小完整demohttps://download.csdn.net/download/junxuezheng/92438853#includeimageplotwidget.h#includeQMouseEvent#includeQDebugImagePlotWidget::ImagePlotWidget(QWidget*parent):QWidget(parent),m_plot(newQCustomPlot(this)),m_selectionRect(newQCPItemRect(m_plot)),m_eventFilter(newEventFilter(this)),m_isSelecting(false),m_rangeRes(1.0),m_azimuthRes(1.0),m_dataWidth(100),m_dataHeight(100){QVBoxLayout*layoutnewQVBoxLayout(this);layout-addWidget(m_plot);setLayout(layout);// 配置 plot// m_plot-setInteractions(QCP::iRangeDrag); // 只允许拖拽禁止缩放可按需添加 iSelectPlottables// m_plot-axisRect()-setRangeDrag(Qt::Horizontal | Qt::Vertical);// 禁用所有鼠标交互m_plot-setInteractions(QCP::iRangeZoom/*iNone*/);// 完全禁止拖拽、缩放、选择等m_plot-xAxis-setLabel(QStringLiteral(距离向 (m)));m_plot-yAxis-setLabel(QStringLiteral(方位向 (m)));setPlotXYAxisColor();// 创建 colorMap 和 colorScale一次性m_colorMapnewQCPColorMap(m_plot-xAxis,m_plot-yAxis);m_colorScalenewQCPColorScale(m_plot);m_plot-plotLayout()-addElement(0,1,m_colorScale);m_colorScale-setType(QCPAxis::atRight);m_colorScale-axis()-setLabel(Pixel Intensity);m_colorMap-setColorScale(m_colorScale);// ✅ 使用单色灰度色图m_colorMap-setGradient(QCPColorGradient::gpGrayscale);// m_colorMap-setGradient(QCPColorGradient::gpJet);// 初始空数据m_colorMap-data()-setSize(10,10);m_colorMap-data()-setCell(10,10,0);m_colorMap-data()-clear();m_colorMap-rescaleDataRange();setcolorScaleAxisColor();// ✅ 配置选择矩形虚线框 半透明填充m_selectionRect-setVisible(true);m_selectionRect-setLayer(overlay);// 提升到顶层m_selectionRect-topLeft-setType(QCPItemPosition::ptPlotCoords);m_selectionRect-bottomRight-setType(QCPItemPosition::ptPlotCoords);m_selectionRect-setPen(QPen(Qt::yellow,1.5,Qt::DashLine));// 黄色虚线边框m_selectionRect-setBrush(QColor(255,255,0,30));// 半透明黄底// m_selectionRect-setPen(QPen(Qt::blue, 10, Qt::DashLine));// m_selectionRect-setBrush(QColor(0, 0, 255, 30));// 初始坐标轴范围m_plot-xAxis-setRange(-100,100);m_plot-yAxis-setRange(-100,100);m_plot-replot();// initEventFilter();connect(m_plot,QCustomPlot::mousePress,this,ImagePlotWidget::mousePressEvent);connect(m_plot,QCustomPlot::mouseMove,this,ImagePlotWidget::mouseMoveEvent);connect(m_plot,QCustomPlot::mouseRelease,this,ImagePlotWidget::mouseReleaseEvent);connect(this,ImagePlotWidget::signal_selectionFinished,this,ImagePlotWidget::slot_selectionFinished);}ImagePlotWidget::~ImagePlotWidget(){deletem_plot;}voidImagePlotWidget::mousePressEvent(QMouseEvent*event){if(event-button()Qt::LeftButton){m_isSelectingtrue;m_startPixelm_plot-mapFromGlobal(event-globalPos());m_endPixelm_startPixel;// 将起始点转换为坐标轴值并设置矩形QPointF startDatapixelToData(m_startPixel);m_selectionRect-topLeft-setCoords(startData.x(),startData.y());m_selectionRect-bottomRight-setCoords(startData.x(),startData.y());m_selectionRect-setVisible(true);m_plot-replot();}QWidget::mousePressEvent(event);}voidImagePlotWidget::mouseMoveEvent(QMouseEvent*event){if(m_isSelecting){m_endPixelm_plot-mapFromGlobal(event-globalPos());QPointF endDatapixelToData(m_endPixel);m_selectionRect-bottomRight-setCoords(endData.x(),endData.y());m_selectionRect-setVisible(true);m_plot-replot();}QWidget::mouseMoveEvent(event);}voidImagePlotWidget::mouseReleaseEvent(QMouseEvent*event){if(event-button()Qt::LeftButtonm_isSelecting){m_isSelectingfalse;// m_selectionRect-setVisible(false);m_plot-replot();emitsignal_selectionFinished();}QWidget::mouseReleaseEvent(event);}