PDA

View Full Version : qwt6.0.1 legend problem



alainstgt
20th March 2012, 22:09
Hello to everyone,
I have written a piece of code to show the result of a frequency spectrum computation.
The curve is well plotted, but I am missing the legend. I tried out several variants, but nothing helped.
See the code below:


// plot object
QwtPlot myPlot;
myPlot.setTitle( "Autospektrum!" );
myPlot.setAxisTitle( QwtPlot::xBottom, "frequency bin" );
myPlot.setAxisTitle( QwtPlot::yLeft, "log Amplitude" );
myPlot.setCanvasBackground( QColor( Qt::darkBlue ));

// grid
QwtPlotGrid grid;
grid.enableXMin(true);
grid.setMajPen( QPen( Qt::white, 0, Qt::DotLine ));
grid.setMinPen( QPen( Qt::gray, 0 , Qt::DotLine ));
grid.attach( &myPlot );

// define the curve object
QwtPlotCurve curve;
curve.setSamples( pXData, pResults, FFT_LENGTH );
curve.setPen( QPen( Qt::yellow ));
curve.setRenderHint( QwtPlotItem::RenderAntialiased );
curve.setLegendAttribute( QwtPlotCurve::LegendShowLine );
curve.attach( &myPlot );

// legend
QwtLegend legend;
legend.setItemMode( QwtLegend::ReadOnlyItem );

QwtLegendItem legendItem;
legendItem.setText( QwtText( "Autospektrum" ));
myPlot.insertLegend( &legend, QwtPlot::RightLegend );

myPlot.replot();
myPlot.show();

alainstgt
21st March 2012, 12:51
ich habe inzwischen weiter experimentiert. Füge ich

legendItem.setVisible( true );
hinzu, wird ein neues Fenster geöffnet mit dem Text der Legende, aber ohne Kurvenlinie.
Wer kann Hilfestellung leisten, da ich keine Lösung sehe, ich bin genau so vorgegangen wie im Beispiel BodePlot?
Danke!

Uwe
21st March 2012, 13:24
First of all you should modify your code so that all of your widgets ( maybe beside a top level widget in main ) and plot items are allocated by new.
Then show your main and how the code above is called.

Uwe

alainstgt
29th March 2012, 18:52
I have had time to further look at my problem and find the solution.
In reality I didn´t see that the line of the curve within the legend was plotted, because the line has a light yellow colour and the background is light gray. The line is also very short.
I added a curve title which is now displayed as assumed and so fix the view onto the legend.
I have attached the revised code below.

Uwe, I am aware that in a normal case it is necessary to allocate the memory on the heap, this was a fast evaluation how to mix SigLib and Qwt, and I only had a main function, so allocating on the stack was OK for that peculiar purpose.
Anyway, now I am going to clone the SigLib graphics function written with SignalVisualize/wxWidgets with Qt/Qwt, and there of course I´ll use the approach recommended by you.

// plot object
QwtPlot myPlot;
myPlot.setTitle( "Autospektrum!" );
myPlot.setAxisTitle( QwtPlot::xBottom, "frequency bin" );
myPlot.setAxisTitle( QwtPlot::yLeft, "log Amplitude" );
myPlot.setCanvasBackground( QColor( Qt::darkBlue ));

// grid
QwtPlotGrid grid;
grid.enableXMin( true );
grid.setMajPen( QPen( Qt::white, 0, Qt::DotLine ));
grid.setMinPen( QPen( Qt::gray, 0 , Qt::DotLine ));
grid.attach( &myPlot );

// define the curve object
QwtPlotCurve curve;
curve.setSamples( pXData, pResults, FFT_LENGTH );
curve.setPen( QPen( Qt::yellow ));
curve.setRenderHint( QwtPlotItem::RenderAntialiased );
curve.setTitle("Sinus");
curve.setLegendAttribute( QwtPlotCurve::LegendShowLine );
curve.attach( &myPlot );

// legend
QwtLegend legend;
myPlot.insertLegend( &legend, QwtPlot::RightLegend );

//myPlot.replot();
myPlot.show();