PDA

View Full Version : QwtLegendData : how to retrieve the pen color of the curve ?



dasycarpum
14th September 2014, 15:08
Hi,

I want to recover the color of the curve for the background legend (<--- ) but any success...:confused: For the title, it's OK :)


void Legend::updateItem( QStandardItem *item, const QwtLegendData &data )
{
const QVariant titleValue = data.value( QwtLegendData::TitleRole );
const QVariant colorValue = data.value(QwtLegendData::IconRole); <---

QwtText title;
if ( titleValue.canConvert<QwtText>() )
{
item->setText( title.text() );
title = titleValue.value<QwtText>();
}
else if ( titleValue.canConvert<QString>() )
{
title.setText( titleValue.value<QString>() );
}
item->setText( title.text() );
item->setBackground(QBrush(QColor(colorValue.Color))); <---

const QVariant iconValue = data.value( QwtLegendData::IconRole );

QPixmap pm;
if ( iconValue.canConvert<QPixmap>() )
pm = iconValue.value<QPixmap>();

item->setData(pm, Qt::DecorationRole);
}

Thank you for your lights

Uwe
14th September 2014, 17:39
Assuming that you want implement your own type of legend, that has nothing to do with QwtLegend: instead of trying to reverse engineer information from an icon ( usually a QwtGraphic ) I would pass the information you need - exactly like you need it.
As QwtLegendData is simply a couple of QVariants, you can overload QwtPlotCurve::legendData() and return whatever you want. All you have to take care of is that your legend is able to understand them.

You could also append to the default data:


virtual QList<QwtLegendData> YourCurve::legendData() const
{
QList<QwtLegendData> data = QwtPlotCurve::legendData();

// 0: a curve - like most plot items - has only one entry on the legend
data[0].setValue( QwtLegendData::UserRole + 1, QVariant( this->pen().color() ) );

return data;
}Of course you can do the same with any type of plot item.

Uwe

dasycarpum
14th September 2014, 19:38
Thank you for your quick and detailed response; I try it and I inform you of the results !

dasycarpum
17th December 2014, 09:55
Hi,

Here is a complete code that works :

mycurve.h


class MyCurve : public QwtPlotCurve
{
public:
MyCurve(){}
~MyCurve(){}

virtual QList<QwtLegendData>legendData(void) const;
};


mycurve.cpp


QList<QwtLegendData> MyCurve::legendData(void) const
{
QList<QwtLegendData> data = QwtPlotCurve::legendData();

data[0].setValue(QwtLegendData::UserRole + 1, QVariant(this->pen().color()));

return data;
}

...MyCurve *curve = new MyCurve();
...
curve->setPen(QColor(...,...,...);
curve->legendData();
...
curve->attach(this); ...

void Legend::updateItem( QStandardItem *item, const QwtLegendData &data )
{
...
const QVariant colorValue = data.value(QwtLegendData::UserRole + 1);
QColor curveColor;
if (colorValue.canConvert<QColor>())
curveColor = colorValue.value<QColor>();

item->setBackground(QBrush(QColor(curveColor)));
}


:cool: