QCustomPlot-用鼠标矩形框进行框选放大

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();
    }

}

在这里插入图片描述

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过鼠标轨迹画矩形框,可以结合使用QCustomPlot鼠标事件和QCPItemRect类。可以按照以下步骤进行操作: 1. 创建一个QCPItemRect对象: ``` QCPItemRect *rect = new QCPItemRect(customPlot); rect->setPen(QPen(Qt::red)); rect->setBrush(QBrush(Qt::blue)); ``` 2. 实现鼠标事件: ``` void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->topLeft->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->setVisible(true); ui->customPlot->replot(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() == Qt::LeftButton) { rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); ui->customPlot->replot(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->setVisible(false); ui->customPlot->replot(); } } ``` 其中,鼠标按下时记录矩形框左上角的坐标,鼠标移动时更新矩形框右下角的坐标并重新绘制,鼠标释放时隐藏矩形框并重新绘制。 完整的代码示例: ``` void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->topLeft->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->setVisible(true); ui->customPlot->replot(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() == Qt::LeftButton) { rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); ui->customPlot->replot(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->setVisible(false); ui->customPlot->replot(); } } void MainWindow::setupPlot() { // ... rect = new QCPItemRect(ui->customPlot); rect->setPen(QPen(Qt::red)); rect->setBrush(QBrush(Qt::blue)); rect->setVisible(false); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值