其他分享
首页 > 其他分享> > QCustomPlot 对鼠标悬停的轴进行缩放

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

标签:drawPen,PRPDView,Qt,缩放,axisRect,QCustomPlot,yAxis2,鼠标悬停,360
来源: https://blog.csdn.net/qq_41923833/article/details/123170910