PDA

View Full Version : Qwt Legend item/entry set checked programmatically. How?



JackieN
4th September 2014, 16:23
I can check an entry in a qwt_legend by a mousecklick but how can I achive this programmatically.
I can hide or show a plotcurve but the representation of the curve in the legend is always unchecked

I searched the internet for a clue but found nothing really usefull.

JackieN
5th September 2014, 09:03
Sorry, I forgot to write some example code.
My Code


QwtLegend *legend = new QwtLegend;
legend->setDefaultItemMode( QwtLegendData::Checkable );
m_plotter->insertLegend( legend, QwtPlot::RightLegend );
connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ), SLOT( legendChecked( const QVariant &, bool ) ) );
QwtPlotCurve *curve = new QwtPlotCurve(name);
curve->setItemAttribute(QwtPlotItem::Legend, true);
curve->setPen( color );
curve->attach( m_plotter );


I can show the curve by code but the legenditem isn't automatically checked.

Added after 1 3 minutes:

With this semi "solution" I can check every item in the legend but I want to check only specific items in the legend
QObjectList obj_lst = m_plotter->legend->contentsWidget()->children();
int sz = obj_lst.size();
for(QObject* obj : obj_lst)
{
QString str = obj->objectName();
std::string stdStr = str.toStdString();
QwtLegendLabel *lgdItem = dynamic_cast<QwtLegendLabel*>(obj);

if(lgdItem != NULL)
lgdItem->setChecked(true);
}

Uwe
5th September 2014, 09:08
Setting a legend item checked is done in several Qwt examples, where you can find the code. F.e the tvplot example - or in scaleengine/transformplot you even find a method "setLegendChecked( QwtPlotItem * )", that does an exclusive check ( unchecking all others ).
The missing link between plot items and legend items is to use QwtPlot::itemToInfo().

Uwe