PDA

View Full Version : Legend identifiers disappear after inserting widget



orignihn
26th May 2011, 22:29
Hello all,

I'm using Qwt 5.2.1 to plot some data in a subclass of QwtPlot. I have the following code:



QwtPlotCurve* curve = new QwtPlotCurve("Variable");
curve->attach(this);

legend()->insert(curve, new QLabel());


There's a bunch of other code being executed, but the problem seems to be the legend()->insert() call. If I comment that line out, I get a nice display of the curve's line, symbol, and text (as per the identifierMode() that is set). If that line is left in, the line and symbol display disappear. I check the identifierMode() on each plot update, and it is equal to 7 (QwtLegendItem::ShowLine | QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText) each time. When the insert() call is left in, I can update the QLabel just fine.

How do I get both the QLabel and the curve's symbol and line into the legend?

Thanks,
- Tom

Uwe
27th May 2011, 07:31
There is only one widget allowed ( it can be a composite one ) , that represents a plot item on the legend. Inserting a QLabel replaces but doesn't add something. Of course you could use a dummy plot item ( maybe a NULL works too - but I have never tries this ).

Also consider to reimplement YourCurve::legendItem/updateLegend instead of inserting a label manually.

Uwe

orignihn
27th May 2011, 16:08
Also consider to reimplement YourCurve::legendItem/updateLegend instead of inserting a label manually.

I'll have to try that, thanks.


There is only one widget allowed ( it can be a composite one ) , that represents a plot item on the legend. Inserting a QLabel replaces but doesn't add something.

At one point I (accidentally) had multiple labels being displayed for the same plot item. I didn't try updating them all, but I had several labels per plot item all with the same text. Maybe I'll try reproducing that.

Thanks,
- Tom

orignihn
3rd June 2011, 14:50
I tried subclassing QwtPlotCurve, and it seems to ALMOST be working. Here's my code for MyQwtPlotCurve:



// MyQwtPlotCurve.h //
class MyQwtPlotCurve : public QwtPlotCurve
{
public:
MyQwtPlotCurve();
MyQwtPlotCurve(const QString& title);

void updateLegend(QwtLegend* legend);
void enable_legend_value(bool on);
void set_legend_value(double value);

private:
bool m_value_enabled;
double m_value;
};


and the relevant part of the cpp file:



void MyQwtPlotCurve::updateLegend(QwtLegend* legend)
{
QwtPlotCurve::updateLegend(legend);

printf("Update legend!\n")

if (m_value_enabled)
{
QWidget *widget = legend->find(this);
if ( !widget || !widget->inherits("QwtLegendItem") )
return;

QwtLegendItem* legendItem = (QwtLegendItem*)widget;

QString text = legendItem->text().text();
QString new_text = text + " " + QString().setNum(m_value);
printf("Trying to set marker text: '%s'\n", new_text.toLocal8Bit().constData());
legendItem->setText(new_text);

legendItem->update();
}
}


That first printf is never printed unless I call updateLegend explicitly. I can then see a flicker as my extra text appears, but it seems to be immediately overwritten.

I'm only using instances of MyQwtPlotCurve, so why isn't my updateLegend() being used?

I'm using Qwt 5.2.1.

Thanks,
- Tom

EDIT: Found my error! Forgot the "const" modifier on MyQwtPlotCurve::updateLegend().