一、先看效果圖
框取數據後,還可以按Ctrl追加數據點
二、原理
qcustomPlot是有鼠標選擇框功能的,只是默認是不使能,所以平常拖動鼠標沒有反應,調用setSelectionRectMode可以開啓選框,如下開啓:
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//模式:框選放大
QCP::SelectionRectMode有幾個枚舉值
QCP::SelectionRectMode | 說明 |
srmNone | 選擇矩形被禁用 |
拖動鼠標選擇框變爲活動,放開時縮放軸的範圍 | |
拖動鼠標時,選擇框變爲活動。在釋放時,如果繪圖表的可選性設置允許,則選擇選擇矩形內的繪圖表數據點 | |
連接到選擇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 | 說明 |
圖表不可選 | |
選擇的行爲類似於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個graph
customPlot->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;
//清空listwidget
ui->lst_data->clear();
for(int i=0;i<customPlot->graphCount();i++)
{
//遍歷有被選中的graph
if(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);
//添加到listwidget
ui->lst_data->addItem("曲線"+str_at+":"+str_key+", "+str_value);
}
}
}
}
//滾動到底部
ui->lst_data->scrollToBottom();
}