Segfault when adding Qwt plot to layout

2019-09-06 11:01发布

I try to build chart demo using Qwt and C++. I've added the following code to button click handler:

QwtPlot *plot = new QwtPlot(QwtText("Demo"));
plot->setGeometry(0, 0, 100, 100);
QwtPlotCurve curve("Sine");
QVector<double> xs;
QVector<double> ys;
for (double x = 0; x < 100; x++)
{
    xs.append(x);
    ys.append(sin(x));
}
QwtPointArrayData *series = new QwtPointArrayData(xs, ys);
curve.setData(series);
curve.attach(plot);
plot->show();
QLayout *lay = ui->centralWidget->layout();
lay->addWidget(plot);

and it segfaults at addWidget(plot);.

What am I doing wrong?

1条回答
萌系小妹纸
2楼-- · 2019-09-06 11:42

The layout of centralWidget is probably NULL or you haven't initialized the ui (e.g. calling setUp()). Check the first case with if(lay == NULL) and the second by looking at your code.

If the first case is correct have a look at your UI file in QDesigner and add a layout.

查看更多
登录 后发表回答