PDA

View Full Version : QwtPlot (v5.1.1) looses connection to legend after manual clear() and reload



EliGri
4th December 2008, 22:29
QwtPlot (v5.1.1) looses connection to legend after I manually clear() and reload legend.
Can't toggle curves off/on.

I've provided this function so users can collapse the legend down to only those curves that are visible in QwtPlot area.

QwtPlot legendChecked(QwtPlotItem*, bool) signal doesn't appear to be "Emitted" after my simplifyLegend() method (see src below) is called. I suspect this because I have a slot that's connected to the legendChecked() signal, and it's never entered if my simplifyLegend function is called prior to the legendChecked event.
Note: Everything works fine (I can toggle curves off/on) if I don't call my simplifyLegend().

Any Ideas?

Here's the function:

void SingleLogTrack::simplifyLegend(bool on) {

m_plotItems = itemList();
m_legend->clear();

foreach (QwtPlotItem *plt_item, m_plotItems) {
QwtLegendItem* lgd_item = new QwtLegendItem();
if ( plt_item->rtti() == QwtPlotItem::Rtti_PlotCurve ) {
QwtPlotCurve *c = (QwtPlotCurve*)(plt_item);
lgd_item->setText(c->title().text());
lgd_item->setSymbol(c->symbol());
lgd_item->setCurvePen(c->pen());
lgd_item->setItemMode(QwtLegend::CheckableItem);
if (c->style() == QwtPlotCurve::NoCurve)
lgd_item->setIdentifierMode(QwtLegendItem::ShowSymbol);
else
lgd_item->setIdentifierMode(QwtLegendItem::ShowLine);
lgd_item->setChecked(c->isVisible());
}

if (plt_item->title().text() != "Marker" &&
plt_item->title().text() != "Grid") {
if (on) {
if (plt_item->isVisible()) m_legend->insert(plt_item,lgd_item);
} else {

m_legend->insert(plt_item,lgd_item);
}
}
}
replot();

}

Uwe
5th December 2008, 08:23
Your code doesn't connect the clicked/checked signals of the legend item with the legendItemClicked/legendItemChecked slots of the plot widget.


I've provided this function so users can collapse the legend down to only those curves that are visible in QwtPlot area.

Better use curve->setItemAttribute(QwtPlotItem::Legend, ...) instead. F.e. overload QwtPlotCurve::setVisible() and toggle the flag together with it.

Uwe

EliGri
5th December 2008, 21:11
Uwe, got it working. Thanks for the reply!

I was successful implementing curve->setItemAttribute(QwtPlotItem::Legend,...) in my simplifyLegend() function.

Here's the new version of the function:

void SingleLogTrack::simplifyLegend(bool on) {
bool showit = true;
foreach (QwtPlotItem *plt_item, itemList()) {
if ( plt_item->rtti() == QwtPlotItem::Rtti_PlotCurve ) {
QwtPlotCurve *c = (QwtPlotCurve*)(plt_item);
if (on) showit = c->isVisible();
c->setItemAttribute(QwtPlotItem::Legend,showit);
}
}
}

The only issue I can see is that when the legend items are put back into the legend, they appear to be split into two groups: visible curves and hidden curves; consequently, they're not listed in the same order that was originally loaded (and stored in the QwtPlotItemList). Not a big problem however. And I can live with it.

Thank you again