Hello
I would like to sort my plot legend alphabetically. Therefore I imlemented following class inheriting from QwtLegend assuming that the virtual legendItems(void) method is called by the plot.
{
public:
{
}
QList<QWidget*> legendItems(void) const
{
QList<QWidget
*> lLegendItems
= QwtLegend::legendItems();
qSort(lLegendItems.begin(), lLegendItems.end(), Legend::lessThan);
return lLegendItems;
}
{
if(pLi1 && pLi2)
{
if (pLi1->inherits("QwtLegendItem") && pLi2->inherits("QwtLegendItem"))
{
return lLabel1->text().text().toLower() < lLabel2->text().text().toLower();
}
}
return false;
}
};
class Legend : public QwtLegend
{
public:
Legend(QWidget* pParent = 0) : QwtLegend(pParent)
{
}
QList<QWidget*> legendItems(void) const
{
QList<QWidget*> lLegendItems = QwtLegend::legendItems();
qSort(lLegendItems.begin(), lLegendItems.end(), Legend::lessThan);
return lLegendItems;
}
static bool lessThan(QWidget* pLi1, QWidget* pLi2)
{
if(pLi1 && pLi2)
{
if (pLi1->inherits("QwtLegendItem") && pLi2->inherits("QwtLegendItem"))
{
QwtLegendItem* lLabel1 = (QwtLegendItem*)pLi1;
QwtLegendItem* lLabel2 = (QwtLegendItem*)pLi2;
return lLabel1->text().text().toLower() < lLabel2->text().text().toLower();
}
}
return false;
}
};
To copy to clipboard, switch view to plain text mode
The legend is added to the plot...
Legend* lLegend = new Legend(mPlot->canvas());
// some initialisations...
mPlot
->insertLegend
(lLegend,
QwtPlot::ExternalLegend);
Legend* lLegend = new Legend(mPlot->canvas());
// some initialisations...
mPlot->insertLegend(lLegend, QwtPlot::ExternalLegend);
To copy to clipboard, switch view to plain text mode
But the legendItems(void) method is never called.
Any idea what's wrong?
Best Regards
Stefan
Bookmarks