Reputation: 854
I have a problem, recently I've installed qwt library to draw graph and was running through some simple tutorials( http://www.youtube.com/watch?v=8l2VIooQXVc this tutorial to be exact, that I have ran into trouble ) and have met a problem. My graph won't paint. Qwt just paints coordinate system and title.The difference between my code and tutorial code, that he paints straight in main, but I do this in QMainWindow, adding plot to QLayout. Here's my code:
void MainWindow::PrioritizeGraphic()
{
plot = new QwtPlot( QwtText( "Demo" ) );
plot->setAxisScale( QwtPlot::xBottom, 0.0, 2.0 * M_PI * 10 );
plot->setAxisScale( QwtPlot::yLeft, -3.0, 3.0 );
int i = 0;
double xl[200], yl[200];
QwtPlotCurve curve( "Sine curve" );
while( i < 100 )
{
for( double x = 0; x < 2.0 * M_PI * 5; x += ( M_PI / 10.0 ) )
{
yl[i] = sin(x);
xl[i] = x;
i++;
curve.setRawSamples( ( const double* )&xl, ( const double* )&yl, i );
curve.attach( plot );
}
}
curve.setRawSamples( ( const double* )&xl, ( const double* )&yl, 100 );
curve.attach( plot );
plot->show();
}
And in the QMainWindow constructor:
QLayout* lay = ui->centralWidget->layout();
lay->addWidget( plot );
How can I deal with this?
Upvotes: 0
Views: 225
Reputation: 2073
Reason for your code is not working is; below variables are created in the scope of function MainWindow::PrioritizeGraphic()
:
double xl[200]
double yl[200]
QwtPlotCurve curve( "Sine curve" )
Since they are defined in the scope of a function, they will be destroyed when function finishes. As a solution you can make them class members so they will survive as long as your MainWindow
object is not destroyed.
The code you referenced works because variables are defined in main()
function and main()
function doesn't finish as long as application runs.
Also you can use QwtPlotCurve::setSamples
instead of QwtPlotCurve::setRawSamples
. It will make a deep copy of the data, so you can freely delete your arrays.
Upvotes: 1