0

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?

Michael
  • 706
  • 9
  • 29

2 Answers2

1

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

Uwe
  • 705
  • 3
  • 3
0
  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.

George Y.
  • 11,307
  • 3
  • 24
  • 25
  • From the hints above only part 3 (Plotting the title) works... Still do not get what the problem is. – Michael Dec 14 '13 at 17:13
  • By "works" do you mean that you can see the title on the plot widget, but not the plot itself? Do you see the axis? Please consider posting a compilable sample which reproduces the problem or upload the project somewhere, as the problem may be somewhere else (like UI file) . – George Y. Dec 15 '13 at 00:29
  • OK, I will provide an example as soon I have time. What I meant in the comment before, is that I can see the Title. I am can also see the plot area, the axis and I can change the color of the plot axis. But I can not change the scale of both axes (Poit two of your answer). And, most important, the curve is not showing up... Thanks for your comment! – Michael Dec 15 '13 at 09:53
  • See the edit above, looks like this is the source of the problem. – George Y. Dec 15 '13 at 19:33