3

I am using QSplitter where the right pane is QCustomPlot which shows a graph when I click in the left pane (a tree view). The problem is the graph doesn't show up or updates until I resize the splitter. I am using Qt example code:

void MyDialog::setupPlot(QCustomPlot *customPlot)
{
    QString demoName = "Quadratic Demo";
    // generate some data:
    QVector<double> x(101), y(101); // initialize with entries 0..100
    for (int i=0; i<101; ++i)
    {
        x[i] = i/50.0 - 1; // x goes from -1 to 1
        y[i] = x[i]*x[i];  // let's plot a quadratic function
    }
    // create graph and assign data to it:
    customPlot->addGraph();
    customPlot->graph(0)->setData(x, y);
    // give the axes some labels:
    customPlot->xAxis->setLabel("x");
    customPlot->yAxis->setLabel("y");
    // set axes ranges, so we see all data:
    customPlot->xAxis->setRange(-1, 1);
    customPlot->yAxis->setRange(0, 1);
}

The plot of course shows up when I call this function in the constructor (like it is in the example) but not if call this in button clicked.

What do I need to do to make sure the graph is plotted when I call this function?

Nejat
  • 31,784
  • 12
  • 106
  • 138
zar
  • 11,361
  • 14
  • 96
  • 178

1 Answers1

3

You should use replot() function to update the plot :

customPlot->replot();

It causes a complete replot into the internal buffer. This method must be called when you make changes on the axis ranges or data points of graphs. This makes the changes visible.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thanks, that worked, do you know why the update(), repaint() doesn't work? – zar Feb 04 '15 at 18:48
  • 4
    Update or repaint does not make use of the internal structures, buffers,... of QCustomPlot. You should call `replot` to make everything ready. It internally calls `update`. – Nejat Feb 04 '15 at 18:53