我想绘制每 100 毫秒出现的大量数据 (3k)。我试过 QCustomPlot和 Qwt精确的 3k 点,我在使用 Qwt 绘图时表现非常好,而使用 QCustomPlot 时表现非常糟糕。我认为我在使用 QCustomPlot 时行为不当,我使用此代码在 QCustomPlot 中绘图(此示例来自 QCustomPlot plot-examples,我编辑了函数 setupQuadraticDemo
):
void MainWindow::setupQuadraticDemo(QCustomPlot *customPlot)
{
demoName = "Quadratic Demo";
customPlot->addGraph();
customPlot->setNotAntialiasedElements(QCP::AntialiasedElement::aeAll);
customPlot->xAxis->setRange(0, 1000);
customPlot->yAxis->setRange(0, 1000);
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
connect(&dataTimer, &QTimer::timeout, this, [customPlot]
{
constexpr auto length = 3000;
QVector<double> x(length), y(length);
std::srand(std::time(nullptr));
for (int i = 0; i < length; ++i)
{
x[i] = std::rand() % 1000;
y[i] = std::rand() % 1000;
}
customPlot->graph(0)->setData(x, y, true);
customPlot->replot();
});
dataTimer.start(100);
}
和this code对于 Qwt。我对 QCustomPlot 做错了吗?为什么绘图速度太慢?
最佳答案
我想问题的根源在于代码本身。您正在以错误的方式更新点。您必须从代码中删除以下行
std::srand(std::time(nullptr));
此行将强制 rand()
的种子在确定的时间内固定(如果我想准确地说,您的种子值固定为 1 秒
),因此无论数据本身是否更新,您都看不到任何变化,因为重绘将在该持续时间内绘制相同的点(1 sec
)。
关于c++ - 为什么 QCustomPlot 在绘制大数据时速度太慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64489118/