AristoFane
AristoFane

Reputation: 11

Weird behavior of QCustomPlot when dragging with log scale

So I'm making a frequency plot in the Qt framework (ver. 6.6.3) with QCustomPlot 2.0, and I want to be able to zoom and drag the plot along the X-axis, which for lisibility is in log scale. On top of that, I want to fix limits for maximum dezooming and dragging so you don't get lost while navigating through the plot.

But when I run the app I get some weird behavior : when I drag to the right and reach the upper limit, the plot suddenly rescales to the full plot (from lower limit to upper limit) where it should just "block" the dragging action without rescaling the plot. On the left side, something weirder happens : when I drag to the lower limit, it stretches the view the more I drag to the left, like some kind of zoom. The thing is : I use the same scale limiting function on another graph that doesn't have any log scale. And on this non-log graph, everything works properly. So I'm beginning to think that the log scale in QCustomPlot is buggy, but before getting to that conclusion I'd rather see where I'm wrong in my code.

Here is the scale limiting function code, which I found on another stackoverflow topic () :

void MainWindow::limXZoomFreq(QCPRange range){
    const double lowbound = 20;
    const double upbound = 22000;
    QCPRange fixedRange(range);

    if (fixedRange.lower < lowbound){ //
        fixedRange.lower = lowbound;
        fixedRange.upper = lowbound + range.size();
        if (fixedRange.upper > upbound)
        { fixedRange.upper = upbound; }
        ui->freq_plot->xAxis->setRange(fixedRange);
    } else if (fixedRange.upper > upbound){
        fixedRange.upper = upbound;
        fixedRange.lower = upbound - range.size();
        if (fixedRange.lower < lowbound)
        { fixedRange.lower = lowbound; }
        ui->freq_plot->xAxis->setRange(fixedRange);
    }
    return;
}

I connect the function to the corresponding signal :

connect(ui->freq_plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(limXZoomFreq(QCPRange)));

And here is how I define my plot :

    QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
    ui->freq_plot->xAxis->setScaleType(QCPAxis::stLogarithmic);
    ui->freq_plot->xAxis->setTicker(logTicker);
    ui->freq_plot->xAxis->setRange(20, 22000);

    ui->freq_plot->addGraph()->setData(xfreq, yfreq);
    ui->freq_plot->graph(0)->rescaleAxes(); // maybe not necssary since I also set the ranges later
    ui->freq_plot->axisRect()->setRangeDrag(Qt::Horizontal);
    ui->freq_plot->axisRect()->setRangeZoom(Qt::Horizontal);
    ui->freq_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    ui->freq_plot->xAxis->setRange(20, 22000);
    ui->freq_plot->yAxis->setRange(0,-120);
    ui->freq_plot->graph(0)->setPen(graphsPen);

    ui->freq_plot->replot();

I even tried to disable the log-scaling on this graph, and the problems were gone. So I know it's in the log scale implementation, either mine or QCustomPlot's.

Upvotes: 0

Views: 70

Answers (0)

Related Questions