轉自:https://blog.csdn.net/yxy244/article/details/100547688
一、先看效果圖
框取數據后,還可以按Ctrl追加數據點
二、原理
qcustomPlot是有鼠標選擇框功能的,只是默認是不使能,所以平常拖動鼠標沒有反應,調用setSelectionRectMode可以開啟選框,如下開啟:
1 customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//模式:框選放大
QCP::SelectionRectMode有幾個枚舉值
QCP::SelectionRectMode | 說明 |
srmNone | 選擇矩形被禁用 |
拖動鼠標選擇框變為活動,放開時縮放軸的范圍 | |
拖動鼠標時,選擇框變為活動。在釋放時,如果繪圖表的可選性設置允許,則選擇選擇矩形內的繪圖表數據點 | |
連接到選擇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 | 說明 |
圖表不可選 | |
選擇的行為類似於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 }