PDA

View Full Version : More information about QwtLegendLabel



Sangfeust
5th February 2015, 14:29
Hello,

I am using the legend to show/hide my curve and it works fine.

But I am looking for a way to set the QwtLegendLabel of the curve to SET when attached to the plot...

Can someone help me?
I have not find how to reach easyli QwtLegendLabel of a curve...

I use this to show/hide:

void GraphB::showCurve( QwtPlotItem *item, bool on )
{
// Show/hide the curve
item->setVisible( on );

// Get QwtLegend .... but why
QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );

// List of widget from QwtLegend associate to item
QList<QWidget *> legendWidgets =
lgd->legendWidgets( itemToInfo( item ) );

// ????
if ( legendWidgets.size() == 1 )
{
// legendLabel is the frame of the legend label
QwtLegendLabel *legendLabel =
qobject_cast<QwtLegendLabel *>( legendWidgets[0] );

// If we have the object check it
if ( legendLabel )
legendLabel->setChecked( on );
}

replot();
}

I am also looking for a method to hide the yAxis of the QwtPlotCurve... if someone has already done something :)

Thanks
Sangfeust

Uwe
8th February 2015, 10:33
tvplot or cpuplot examples show how to access legend labels, axes can be shown/hidden using QwtPlot::enableAxis().

Uwe

Sangfeust
9th February 2015, 09:46
Thanks Uwe for your reply.

Actually I use this 3 function, but I am looking to have just one... But don't find how. Because it is not all clear for me how to get the legendLabel state.



//Standard slot call by legendLabel, so why no pointer to the label?
void GraphB::legendChecked( const QVariant &itemInfo, bool on )
{
QwtPlotItem *plotItem = infoToItem( itemInfo );
if ( plotItem )
showCurve( plotItem, on );
}

//Standard private function called by legendChecked
void GraphB::showCurve( QwtPlotItem *item, bool on )
{
// Montre ou cache la courbe/l'item désiré
item->setVisible( on );

// On récupère le QwtLegend ... mais pourquoi faire?
QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );

// On récupère une liste de widget à partir de QwtLegend
QList<QWidget *> legendWidgets =
lgd->legendWidgets( itemToInfo( item ) );

// ????
if ( legendWidgets.size() == 1 )
{
// legendLabel représente la zone de la légende associé à une courbe
QwtLegendLabel *legendLabel =
qobject_cast<QwtLegendLabel *>( legendWidgets[0] );

// Si on a réussi à créer l'objet on check/uncheck
if ( legendLabel )
legendLabel->setChecked( on );
}

replot();
}

//I use this function to set/reset the legendLabel, I just add the "setVisible( on )" function to have the curve as the legend.
//But I am not sure about the reliability of this funchtion, is there plotItem without "setVisible" method?
void GraphB::setLegendChecked( QwtPlotItem *plotItem , bool on )
{
plotItem->setVisible( on );
const QwtPlotItemList items = itemList();
for ( int i = 0; i < items.size(); i++ )
{
QwtPlotItem *item = items[ i ];
if ( item->testItemAttribute( QwtPlotItem::Legend ) )
{
QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );

QwtLegendLabel *label = qobject_cast< QwtLegendLabel *>(
lgd->legendWidget( itemToInfo( item ) ) );
if ( label )
{
lgd->blockSignals( true );
if( item == plotItem )
label->setChecked( on );//item == plotItem );
lgd->blockSignals( false );
}
}
}
}