一、先看效果图

框取数据后,还可以按Ctrl追加数据点

二、原理

qcustomPlot是有鼠标选择框功能的,只是默认是不使能,所以平常拖动鼠标没有反应,调用setSelectionRectMode可以开启选框,如下开启:

        customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//模式:框选放大

QCP::SelectionRectMode有几个枚举值

QCP::SelectionRectMode 说明
srmNone 选择矩形被禁用

srmZoom

拖动鼠标选择框变为活动,放开时缩放轴的范围

srmSelect

拖动鼠标时,选择框变为活动。在释放时,如果绘图表的可选性设置允许,则选择选择矩形内的绘图表数据点

srmCustom

连接到选择rect的信号(例如QCPSelectionRect::accepted),以便处理用户交互。

(1)设置选框的边框和颜色

    customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//虚线customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//半透明浅蓝

(2)框选放大和拖动无法同时使用

setSelectionRectMode如果设置为srmZoom放大模式, 即使Interaction设置了拖动iRangeDrag,鼠标按下后依然是框选放大,而不会是拖动曲线,如下代码拖动是无效的:

  customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//模式:框选放大
customPlot->setInteraction(QCP::iRangeDrag,true);//使能拖动

原因:跟踪源码,可以看到如果鼠标按下事件里,如果选择框模式不为srmNone,就执行选框操作了,而不会执行Interaction操作。

(3)框选数据

setSelectionRectMode设置为srmSelect选取数据模式,接着还需要setSelectable (QCP::SelectionType selectable)设定数据选取的方式,SelectionType的枚举效果分别如下:

QCP::SelectionType 说明

stNone

图表不可选

stWhole

选择的行为类似于stMultipleDataRanges,但是如果选择了任何数据点,则整个绘图表将被选中

stSingleData

一次可以选择一个单独的数据点

stDataRange

可以选择多个连续数据点(一个数据范围)。

stMultipleDataRanges

可以选择任何数据点/范围的组合。

想要选中数据后再追加数据,默认是按住Ctrl在选择数据,这个按键有需要可以修改Shift、Atl等:

customPlot->setMultiSelectModifier(Qt::KeyboardModifier::ControlModifier);//多选按键Ctrl

最后Interaction还要设置图表可选和多选,才能真正的选取任意曲线的数据点,代码例子:

    //数据多选customPlot->graph(0)->setSelectable(QCP::SelectionType::stMultipleDataRanges);customPlot->graph(1)->setSelectable(QCP::SelectionType::stMultipleDataRanges);customPlot->graph(2)->setSelectable(QCP::SelectionType::stMultipleDataRanges);//选择框模式:选取数据customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmSelect);//选框黑色虚线customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//滚动缩放、图表可选、多选customPlot->setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables| QCP::iMultiSelect);//

三、例子关键代码

第一个动态图例子的关键代码贴在下面,工程已打包,可以直接下载调试

头文件里:

    void on_customplot_selectionChangedByUser();void on_act_zoomIn_toggled(bool arg1);void on_act_move_toggled(bool arg1);void on_act_select_toggled(bool arg1);void contextMenuRequest(QPoint pos);void rescaleAxes();

构造函数里:

 //右键菜单自定义ui->customplot->setContextMenuPolicy(Qt::CustomContextMenu);//信号连接槽函数connect(ui->customplot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));QCustomPlot* customPlot=ui->customplot;//四边显示坐标轴customPlot->axisRect()->setupFullAxesBox();// 生成数据:int n=100;QVector<double> x(n), y1(n),y2(n),y3(n); //for (int i=0; i<n; i+=2){x[i] = i; //y1[i] = qSin(i/(double)n*M_PI*2); //y2[i] = qCos(i/(double)n*M_PI*2); //y3[i] = (i<=50)? i/(double)50*2-1 : -(i-50)/(double)50*2+1;}// 创建3个graphcustomPlot->addGraph();customPlot->graph(0)->setData(x, y1);customPlot->addGraph();customPlot->graph(1)->setData(x, y2);customPlot->addGraph();customPlot->graph(2)->setData(x, y3);//曲线颜色customPlot->graph(0)->setPen(QPen(Qt::green,2));customPlot->graph(1)->setPen(QPen(Qt::red,2));customPlot->graph(2)->setPen(QPen(Qt::gray,2));//连接方式customPlot->graph(0)->setLineStyle((QCPGraph::LineStyle::lsImpulse));//脉冲线//customPlot->graph(1)->setLineStyle((QCPGraph::LineStyle::lsStepCenter));//阶梯线,左对齐// customPlot->graph(2)->setLineStyle((QCPGraph::LineStyle::lsStepLeft));//阶梯线,左对齐//不显示连线//customPlot->graph(0)->setLineStyle(QCPGraph::LineStyle::lsNone);customPlot->graph(1)->setLineStyle(QCPGraph::LineStyle::lsNone);customPlot->graph(2)->setLineStyle(QCPGraph::LineStyle::lsNone);//数据点显示图案customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc,8));customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssCross,8));customPlot->graph(2)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssTriangle,8));//自动调整范围customPlot->graph(0)->rescaleAxes();customPlot->graph(1)->rescaleAxes(true);customPlot->graph(2)->rescaleAxes(true);//数据多选customPlot->graph(0)->setSelectable(QCP::SelectionType::stMultipleDataRanges);customPlot->graph(1)->setSelectable(QCP::SelectionType::stMultipleDataRanges);customPlot->graph(2)->setSelectable(QCP::SelectionType::stMultipleDataRanges);//选择框模式:无customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);//选框黑色虚线customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//修改多选按键,默认Ctrl//customPlot->setMultiSelectModifier(Qt::KeyboardModifier::ControlModifier);//滚动缩放、图表可选、多选customPlot->setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables| QCP::iMultiSelect);//customPlot->replot();

放大、拖动、选择action槽函数:

//放大action
void MainWindow::on_act_zoomIn_toggled(bool arg1)
{QCustomPlot* customPlot=ui->customplot;if(arg1){ui->act_move->setChecked(false);//取消拖动选项customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动ui->act_select->setChecked(false);//取消选择customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);}else{customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);}
}//拖动action
void MainWindow::on_act_move_toggled(bool arg1)
{QCustomPlot* customPlot=ui->customplot;if(arg1){ui->act_zoomIn->setChecked(false);//取消放大ui->act_select->setChecked(false);//取消选择customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);customPlot->setInteraction(QCP::iRangeDrag,true);//使能拖动}else{customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动}
}//选择action
void MainWindow::on_act_select_toggled(bool arg1)
{QCustomPlot* customPlot=ui->customplot;if(arg1){ui->act_zoomIn->setChecked(false);//取消放大ui->act_move->setChecked(false);//取消拖动选项customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmSelect);}else{customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);}
}

选择变化槽函数:

//选择的数据变化
void MainWindow::on_customplot_selectionChangedByUser()
{QCustomPlot* customPlot=ui->customplot;//清空listwidgetui->lst_data->clear();for(int i=0;i<customPlot->graphCount();i++){//遍历有被选中的graphif(customPlot->graph(i)->selected()){QCPDataSelection selection =customPlot->graph(i)->selection();//遍历选中范围for(int j=0;j<selection.dataRangeCount();j++){QCPDataRange dataRange = selection.dataRange(j);//遍历数据for(int k=dataRange.begin();k<dataRange.end();k++){QString str_key = QString::number(customPlot->graph(i)->data()->at(k)->key);QString str_value = QString::number(customPlot->graph(i)->data()->at(k)->value);QString str_at= QString::number(i);//添加到listwidgetui->lst_data->addItem("曲线"+str_at+":"+str_key+", "+str_value);}}}}//滚动到底部ui->lst_data->scrollToBottom();
}

四、下载

点击下载例程

QCustomPlot使用心得六:框选放大,拖动,选中数据相关推荐

  1. QCustomPlot系列 进行框选放大(二)

    前面一章已经记录了 怎么进行框选放大了. 添加如下代码: customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine ...

  2. QT实现图片的滚轮缩放、框选放大、拖拽移动

    序言 QT用来做界面程序是真的方便,它本身封装了很多类库,需要的时候直接拿来用就行. QT的类继承关系网上有很多相关的文章,这里不做赘述.但是简单的界面程序最主要的继承关系还是如下图: 可以将QT的类 ...

  3. 三个选择框,当前框选过之后的数据其他两个不能选择

    效果图 <template><div class="multi-distribution-head"><c-product:selectObj=&qu ...

  4. matlab算概率,用matlab计算概率,再次吐槽某些吧友国战比赛七框选将的建议

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 国战比赛讲求观赏性,一国独大根本谈不上观赏性.大魏国的出现,和选将框的多少有非常直接关系.我发现吧里不少高等级玩家建议国战比赛七框选将,我很不赞同,这就是 ...

  5. 带你了解P600吊舱是如何实现对特定目标,进行框选追踪跟随的

    今天我们来讲一讲P600室外无人机亮点功能之一"吊舱框选跟踪".该平台配置USB接口的Q10F十倍变焦单光吊舱,并开发了其专用ROS驱动:通过机载系统中内置的KCF框选追踪算法,基 ...

  6. 使用D3.js实现框选节点并进行多节点拖动

    最近再使用d3.js关系图形展示时,需要选中多节点并进行拖动,一开始并不知道D3提供了此API,下面是我结合项目业务整理的框选操作的重点方面的应用. 这是d3提供的api:(使用鼠标或触摸选择一维或二 ...

  7. 基于vue和elementUI封装框选表格组件

    前言:主要是 table 表格框选功能 实现功能如下: 表格框选功能 右键操作功能(删除.查看.编辑) 分页功能 表头分类筛选功能 回显功能(显示默认选中数据) 排序功能 行禁用不可选功能 不说废话, ...

  8. C#制作QQ截图的自动框选功能的个人思路(三)自动框选

    效果图: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  9. 【UE4】UE4框选

    目录: 视窗操作篇 反转鼠标中键 自适应视窗缩放速度 解除对选中物体旋转 对屏幕外物体进行位移旋转缩放 物体吸附到模型顶点 屏幕编队(记录当前相机位置和旋转) 落到正下方物体 显示与隐藏 资源操作篇 ...

最新文章

  1. GET请求中URL的最大长度限制总结
  2. 个人网站搭建---godaddy域名+freewebhostingarea免费空间
  3. 企业号微信支付 公众号支付 H5调起支付API示例代码 JSSDK C# .NET
  4. WindowsServer和普通WIN操作系统有什么不同?
  5. CTFshow php特性 web145
  6. numix Docky
  7. ipad如何连接电脑_ipad如何将电脑文件下载到本地?
  8. 【问链财经-区块链基础知识系列】 第三十三课 区块链溯源方案设计-中检集团区块链溯源平台
  9. Qt Creator批注设计
  10. _Blank主页——个人浏览器主页定制
  11. asp.net core 3.0 中使用 swagger
  12. 网络配置 rpm yum
  13. Java—File类详解及实践
  14. 如何使用PyCharm调试Python代码
  15. JDialog简单使用
  16. node.js下mongoose简单操作实例
  17. 激光成像雷达技术 你了解清楚了吗?
  18. 解决无法ping通 127.0.0.1
  19. itunes显示无法更新服务器失败怎么办啊,iTunes更新时出错怎么办?iTunes更新时出错的解决方法...
  20. 动态网页 —— 案例: 爬取qq邮箱

热门文章

  1. SHELL十三问之十三:for、while 与 until 差在哪?
  2. 精致的像素级别的风格转换 ----- Deep Image Analogy
  3. Java中toString方法的重写
  4. TIPS001:Mendeley下载全文及其命令逻辑
  5. 轻松实现文档转换的word转pdf转换器
  6. 使用PL/SQL,如何获得数字的英文拼写呢?
  7. 【GNN报告】耶鲁大学Rex Ying(应智韬): 双曲表示学习与知识图谱
  8. JavaScript基础-练习-页面小游戏剪刀石头布
  9. 速营社:谈谈我们需要怎样的媒体人
  10. “大数据+大会员+一体化+个性化”引领线下零售O2O革命