QCustomPlot 对鼠标悬停的轴进行缩放

2022/2/27 23:29:31

本文主要是介绍QCustomPlot 对鼠标悬停的轴进行缩放,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

功能

当鼠标悬停在某一个轴上时,滚动鼠标滚轮,可实现对该轴进行缩放,其他轴不变。
请添加图片描述
PRPDView 集成自 QCustomPlot

初始化

void PRPDView::creatInit()
{
    this->yAxis2->setVisible(true);//显示x轴2
    this->yAxis2->setRange(0,10);
    //    this->axisRect()->setupFullAxesBox(true);//会影响轴yAxis2显示
    this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom |QCP::iSelectAxes);
    this->xAxis->setRange(0,360);   //相位 0~360
    
    mTemplateCurve = addGraph(this->xAxis,this->yAxis);
    mScatterCurve = addGraph(this->xAxis,this->yAxis2);

    QPen drawPen;
    drawPen.setColor(Qt::blue);
    drawPen.setWidth(2);
    mTemplateCurve->setPen(drawPen);

    drawPen.setColor(Qt::red);
    drawPen.setWidth(4);
    mScatterCurve->setPen(drawPen);
    mScatterCurve->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle));
    mScatterCurve->setLineStyle(QCPGraph::lsNone);

    this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignHCenter);   // 对齐于QCPAxisRect中间的顶部

    connect(this, &PRPDView::mouseWheel, this, &PRPDView::slot_mouseWheel);
}

生成模拟数据

void PRPDView::setDemo()
{
    //生成模板图
    QVector<double> xPoints(360),yPoints(360);
    for(int x0 = 0;x0 < 361 ;x0++){
        xPoints.append(x0);
        yPoints.append(qSin((double)x0/360. * 2 * Pi) * 1000);
    }
    this->templateGraph()->setData(xPoints,yPoints);
    //生成随机散点图
    xPoints.clear();
    yPoints.clear();
    for(int cnt = 0;cnt < 5000;cnt++){
        xPoints.append(QRandomGenerator::global()->bounded(1.) * 360.);
        yPoints.append(QRandomGenerator::global()->bounded(1.) * 10.);
    }
    this->scatterGtaph()->setData(xPoints,yPoints);
    this->rescaleValueAxisinRange();
    this->replot();
}

槽函数

void PRPDView::slot_mouseWheel()
{
    QList<QCPAxis*> axes;
    if (xAxis->axisRect()->rect().contains(QCursor::pos())){
        axes<<xAxis;
        axisRect()->setRangeZoomAxes(axes);
        axisRect()->setRangeZoom(xAxis->orientation());
    }
    else if (yAxis->axisRect()->rect().contains(QCursor::pos())){
        axes<<yAxis;
        axisRect()->setRangeZoomAxes(axes);
        axisRect()->setRangeZoom(yAxis->orientation());
    }
    else if(yAxis2->axisRect()->rect().contains(QCursor::pos())){
        axes<<yAxis2;
        axisRect()->setRangeZoomAxes(axes);
        axisRect()->setRangeZoom(yAxis2->orientation());
    }
    else{
        axes<<yAxis<<xAxis<<yAxis2;
        axisRect()->setRangeZoomAxes(axes);
        axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
    }
}

main

PRPDView *view = new PRPDView();
view->setAttribute(Qt::WA_DeleteOnClose);
view->setDemo();
view->show();


这篇关于QCustomPlot 对鼠标悬停的轴进行缩放的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程