Search code examples
qtzoomingqwt

qwt zoom out with wrong reect


I have a qwt plot, here a simple example, with a QWtPlotZoomer. The zoom in works, but zoom back reset the scale to 0-1000, instead the original scale.

I tried to solve this with (what is the difference between these lines ?)

    zoomer->setZoomBase(false);
    zoomer->zoom(0);

but that has no effect. What needs to be done to get the correct initial scale for the zoomer? A trivial solution is to implement the zoomer after the curve attachment, but in a real work example that is not applicable:

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QDebug>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_zoomer.h>

#include <vector>
using std::vector;

int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    QwtPlot * plot = new QwtPlot();
    plot->setAxisAutoScale(QwtPlot::xBottom);
    plot->setAxisAutoScale(QwtPlot::yLeft);

    QwtPlotZoomer *zoomer;
    zoomer = new QwtPlotZoomer( QwtPlot::xBottom, QwtPlot::yLeft, plot->canvas() );

    // create data
    vector<double> x(100);
    vector<double> y1(x.size());

    for (size_t i = 0; i< x.size(); ++i) { x[i] = int(i)-50; }
    for (size_t i = 0; i< y1.size(); ++i) { y1[i] = pow(double(abs(i-50))/10,2); }

    // first curve
    QwtPlotCurve *curve = new QwtPlotCurve();
    curve->setRawSamples(&x[0], &y1[0], x.size());
    curve->attach( plot );

    zoomer->setZoomBase(false);
    zoomer->zoom(0);

    plot->replot();

    QMainWindow window;
    window.setCentralWidget(plot);
    window.resize(800, 600);
    window.show();

    return a.exec();
}

Solution

  • Create the zoomer after attaching the curves ( having valid data ) or modify your code to zoomer->setZoomBase(true);

    The line zoomer->zoom(0) is pointless and the final replot is not necessary as it is done by the zoomer to initialize its zoom stack.

    When having a zoom base of [0,1000] you usually have initialized the zoom stack of your zoomer with a plot, where the scales have not been calculated before.

    Note that attaching the curves does not update the ranges immediately - it is done before the next replot ( or better QwtPlot::updateAxes() ).