Qt & QWT: QwtPlotCurve - Curve not showing up

2.4k views Asked by At

I want to show a simple graph using QWT and Qt Creator:

Qt version: 4.8.2, Qt Creator: 2.5.2, QWT version: 6.0.0

I added a QwtPlot to my MainWindow (called "myPlot" in the example). Then I have a callback function which is called each time I press a button:

void MainWindow::forwardPlot()
{
    double x[9] = {1,20,30,40,50,60,70,200,500};
    double y[9] = {1,500,3,1,200,100,2,1,0};
    QwtPlotCurve *curve = new QwtPlotCurve();
    curve->setRawSamples(x,y,9);
    curve->attach( ui->myPlot );
    curve->show();
    ui->myPlot->replot();
    ui->label->setText("bla");
}

Compiling works fine... The label is set to "bla", so I know that the callback function is called. But the curve is not displayed. I am able to resize myPlot for example. But showing the curve does not work. Any hints?

2

There are 2 answers

4
George Y. On
  1. You appear not to set the pen color for the curve:

    curve->setPen( QColor( Qt::green ) );

  2. You need to set up the axis so Qwt knows which part of the plot to show:

    ui->myPlot->setAxisScale( QwtPlot::xBottom, 1.0, 500.0 );

    ui->myPlot->setAxisScale( QwtPlot::yLeft, 1.0, 500.0 );

  3. I'd also set the title for the curve to see if your QwtPlot works at all:

    ui->myPlot->setTitle( "Plot title" );

Edit: I've reproduced the issue. setRawSamples requires the data buffers you pass to be valid during the lifetime of the plot. In your case you pass the local buffers which are invalid as soon as forwardPlot() ends

Allocate the buffers in heap instead.

0
Uwe On

Your point arrays are on the stack and will be gone after leaving forwardPlot().