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

转自:https://blog.csdn.net/yxy244/article/details/100547688

 

一、先看效果图

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

二、原理

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

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

QCP::SelectionRectMode有几个枚举值

QCP::SelectionRectMode 说明

srmNone 选择矩形被禁用

srmZoom

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

srmSelect

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

srmCustom

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

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

1     customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//虚线
2     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还要设置图表可选和多选,才能真正的选取任意曲线的数据点,代码例子:

 1     //数据多选
 2     customPlot->graph(0)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
 3     customPlot->graph(1)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
 4     customPlot->graph(2)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
 5     //选择框模式:选取数据
 6     customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmSelect);
 7     //选框黑色虚线
 8     customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));
 9     customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));
10     //滚动缩放、图表可选、多选
11     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();

构造函数里:

 1  //右键菜单自定义
 2     ui->customplot->setContextMenuPolicy(Qt::CustomContextMenu);
 3     //信号连接槽函数
 4     connect(ui->customplot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
 5  
 6     QCustomPlot* customPlot=ui->customplot;
 7     //四边显示坐标轴
 8     customPlot->axisRect()->setupFullAxesBox();
 9     // 生成数据:
10     int n=100;
11     QVector<double> x(n), y1(n),y2(n),y3(n); //
12     for (int i=0; i<n; i+=2)
13     {
14         x[i] = i; //
15         y1[i] = qSin(i/(double)n*M_PI*2); //
16         y2[i] = qCos(i/(double)n*M_PI*2); //
17         y3[i] = (i<=50)? i/(double)50*2-1 : -(i-50)/(double)50*2+1;
18     }
19  
20     // 创建3个graph
21     customPlot->addGraph();
22     customPlot->graph(0)->setData(x, y1);
23     customPlot->addGraph();
24     customPlot->graph(1)->setData(x, y2);
25     customPlot->addGraph();
26     customPlot->graph(2)->setData(x, y3);
27     //曲线颜色
28     customPlot->graph(0)->setPen(QPen(Qt::green,2));
29     customPlot->graph(1)->setPen(QPen(Qt::red,2));
30     customPlot->graph(2)->setPen(QPen(Qt::gray,2));
31     //连接方式
32     customPlot->graph(0)->setLineStyle((QCPGraph::LineStyle::lsImpulse));//脉冲线
33     //customPlot->graph(1)->setLineStyle((QCPGraph::LineStyle::lsStepCenter));//阶梯线,左对齐
34     // customPlot->graph(2)->setLineStyle((QCPGraph::LineStyle::lsStepLeft));//阶梯线,左对齐
35     //不显示连线
36     //customPlot->graph(0)->setLineStyle(QCPGraph::LineStyle::lsNone);
37     customPlot->graph(1)->setLineStyle(QCPGraph::LineStyle::lsNone);
38     customPlot->graph(2)->setLineStyle(QCPGraph::LineStyle::lsNone);
39     //数据点显示图案
40     customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc,8));
41     customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssCross,8));
42     customPlot->graph(2)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssTriangle,8));
43     //自动调整范围
44     customPlot->graph(0)->rescaleAxes();
45     customPlot->graph(1)->rescaleAxes(true);
46     customPlot->graph(2)->rescaleAxes(true);
47     //数据多选
48     customPlot->graph(0)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
49     customPlot->graph(1)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
50     customPlot->graph(2)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
51     //选择框模式:无
52     customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
53     //选框黑色虚线
54     customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));
55     customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));
56     //修改多选按键,默认Ctrl
57     //customPlot->setMultiSelectModifier(Qt::KeyboardModifier::ControlModifier);
58     //滚动缩放、图表可选、多选
59     customPlot->setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables| QCP::iMultiSelect);//
60  
61     customPlot->replot();

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

 1 //放大action
 2 void MainWindow::on_act_zoomIn_toggled(bool arg1)
 3 {
 4     QCustomPlot* customPlot=ui->customplot;
 5     if(arg1)
 6     {
 7         ui->act_move->setChecked(false);//取消拖动选项
 8         customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
 9         ui->act_select->setChecked(false);//取消选择
10         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
11     }
12     else
13     {
14         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
15     }
16 }
17  
18 //拖动action
19 void MainWindow::on_act_move_toggled(bool arg1)
20 {
21     QCustomPlot* customPlot=ui->customplot;
22     if(arg1)
23     {
24         ui->act_zoomIn->setChecked(false);//取消放大
25         ui->act_select->setChecked(false);//取消选择
26         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
27         customPlot->setInteraction(QCP::iRangeDrag,true);//使能拖动
28     }
29     else
30     {
31         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
32         customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
33     }
34 }
35  
36 //选择action
37 void MainWindow::on_act_select_toggled(bool arg1)
38 {
39     QCustomPlot* customPlot=ui->customplot;
40     if(arg1)
41     {
42         ui->act_zoomIn->setChecked(false);//取消放大
43         ui->act_move->setChecked(false);//取消拖动选项
44         customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
45         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmSelect);
46     }
47     else
48     {
49         customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
50     }
51 }

选择变化槽函数:

 1 //选择的数据变化
 2 void MainWindow::on_customplot_selectionChangedByUser()
 3 {
 4     QCustomPlot* customPlot=ui->customplot;
 5     //清空listwidget
 6     ui->lst_data->clear();
 7     for(int i=0;i<customPlot->graphCount();i++)
 8     {
 9         //遍历有被选中的graph
10         if(customPlot->graph(i)->selected())
11         {
12             QCPDataSelection selection =customPlot->graph(i)->selection();
13             //遍历选中范围
14             for(int j=0;j<selection.dataRangeCount();j++)
15             {
16                 QCPDataRange dataRange = selection.dataRange(j);
17                 //遍历数据
18                 for(int k=dataRange.begin();k<dataRange.end();k++)
19                 {
20                     QString str_key = QString::number(customPlot->graph(i)->data()->at(k)->key);
21                     QString str_value = QString::number(customPlot->graph(i)->data()->at(k)->value);
22                     QString str_at= QString::number(i);
23                     //添加到listwidget
24                     ui->lst_data->addItem("曲线"+str_at+""+str_key+", "+str_value);
25                 }
26             }
27         }
28     }
29     //滚动到底部
30     ui->lst_data->scrollToBottom();
31 }

 

四、下载

点击下载例程

posted @ 2021-01-06 11:12  阳光下的小土豆  阅读(3484)  评论(0编辑  收藏  举报