Search code examples
c++qtqwt

recalculate QwtScaleDiv before rendering


I've implemented a QwtPlot which scrolls across the screen as data is added in real-time. Based on user input, an image of the plot is occasionally rendered to a file using QwtPlotRenderer. However, because the axis scrolls during normal operation, the QwtScaleDiv tick marks can look a little wonky at render time (they are right-aligned):

enter image description here

Is there some easy way in which I can recalculate the division prior to rendering so that the first label is on the far left and the last one is on the far right?


Solution

  • This isn't as difficult as it looked at first. Bascially, all you need to do is temporarily replace the axisScaleDiv.

    auto divX = this->axisScaleDiv(xBottom);
    
    double ub = divX.upperBound();
    double lb = divX.lowerBound();
    double numTicks = 11.0;  // 10 even divisions
    
    // you can create minor/medium ticks if you want to, I didn't.
    QList<double> majorTicks;
    for (int i = 0; i < numTicks; ++i)
    {
        majorTicks.push_back(lb + i * ((ub - lb) / (numTicks - 1)));
    }
    
    // set the scale to the newly created division
    QwtScaleDiv renderDivX(divX.lowerBound(), divX.upperBound(), 
        QList<double>(), QList<double>(), majorTicks);  
    this->setAxisScaleDiv(xBottom, renderDivX);
    
    // DO PLOT RENDERING
    QwtPlotRender renderer;
    renderer.renderDocument(...);
    
    // RESOTRE PREVIOUS STATE
    this->setAxisScaleDiv(xBottom, divX);
    this->setAxisScaleDiv(yLeft, divY);
    
    // update the axes
    this->updateAxes();