QCustomPlot-用鼠标矩形框进行框选放大
一、QCustomPlot自带框选放大功能
发现QCustomPlot自带框选放大功能,不需要自己写,直接启用就好了:
plot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//设置选框的样式:虚线
plot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//设置选框的样式:半透明浅蓝
plot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
不过使用官方的框选放大功能,也带来一个毛病,就是鼠标左、右、中,三个键都变成了框选放大,失去了拖拽平移功能,这用起来很不爽,我们改一下官方源码,使之同时具备这种功能:1、左键框选时进行放大,2、滚轮按下并拖拽时,可平移曲线,3、右键单击弹出功能菜单。
为实现这3个功能,首先我们要把官方的平移功能,由左键改到右键上,见下文。然后我们再把官方的框选功能限制在左键上,如下图两处修改。
//设置qcustomplot画图属性,实时
void setupRealtimeDataDemo(QCustomPlot *customPlot);
//添加实时数据槽
void realtimeDataSlot();
private:
QVector<double> x, y0, y1;
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
// add two new graphs and set their look:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
ui->customPlot->addGraph();
ui->customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
ui->customPlot->graph(0)->rescaleAxes(true);
ui->customPlot->graph(1)->rescaleAxes(true);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
customPlot->graph(0)->setName("温区1");
customPlot->graph(1)->setName("温区2");
customPlot->xAxis->setLabel("时间");
customPlot->yAxis->setLabel("℃");
customPlot->legend->setVisible(true);
customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//设置选框的样式:虚线
customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//设置选框的样式:半透明浅蓝
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
}
void MainWindow::OnTimerout()
{
static double i = 0;
fvalue = dev.pv3;
fvalue = fvalue/10;
this->y1.append(fvalue);
i = i + 1.0;
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);
ui->customPlot->replot();
if( i > 1000 ){
i = 1;
x.clear();
y0.clear();
y1.clear();
}
}