QCustomPlot/Widget 不显示 graph/update

QCustomPlot/Widget doesn't show graph/update

我正在使用 QSplitter,其中右窗格是 QCustomPlot,当我单击左窗格(树视图)时它会显示一个图表。问题是在我调整拆分器的大小之前,图表不会显示或更新。我正在使用 Qt 示例代码:

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

当我在构造函数中调用此函数时(就像在示例中一样),该图当然会显示,但如果在单击按钮中调用此函数则不会显示。

我需要做什么来确保在调用此函数时绘制图形?

您应该使用 replot() 函数来更新绘图:

customPlot->replot();

它会导致内部缓冲区的完整重绘。当您对图表的轴范围或数据点进行更改时,必须调用此方法。这使更改可见。