Results 1 to 4 of 4

Thread: QwtLegendData : how to retrieve the pen color of the curve ?

  1. #1
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QwtLegendData : how to retrieve the pen color of the curve ?

    Hi,

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

    Qt Code:
    1. void Legend::updateItem( QStandardItem *item, const QwtLegendData &data )
    2. {
    3. const QVariant titleValue = data.value( QwtLegendData::TitleRole );
    4. const QVariant colorValue = data.value(QwtLegendData::IconRole); <---
    5.  
    6. QwtText title;
    7. if ( titleValue.canConvert<QwtText>() )
    8. {
    9. item->setText( title.text() );
    10. title = titleValue.value<QwtText>();
    11. }
    12. else if ( titleValue.canConvert<QString>() )
    13. {
    14. title.setText( titleValue.value<QString>() );
    15. }
    16. item->setText( title.text() );
    17. item->setBackground(QBrush(QColor(colorValue.Color))); <---
    18.  
    19. const QVariant iconValue = data.value( QwtLegendData::IconRole );
    20.  
    21. QPixmap pm;
    22. if ( iconValue.canConvert<QPixmap>() )
    23. pm = iconValue.value<QPixmap>();
    24.  
    25. item->setData(pm, Qt::DecorationRole);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for your lights

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtLegendData : how to retrieve the pen color of the curve ?

    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:

    Qt Code:
    1. virtual QList<QwtLegendData> YourCurve::legendData() const
    2. {
    3. QList<QwtLegendData> data = QwtPlotCurve::legendData();
    4.  
    5. // 0: a curve - like most plot items - has only one entry on the legend
    6. data[0].setValue( QwtLegendData::UserRole + 1, QVariant( this->pen().color() ) );
    7.  
    8. return data;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Of course you can do the same with any type of plot item.

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    dasycarpum (14th September 2014)

  4. #3
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtLegendData : how to retrieve the pen color of the curve ?

    Thank you for your quick and detailed response; I try it and I inform you of the results !

  5. #4
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtLegendData : how to retrieve the pen color of the curve ?

    Hi,

    Here is a complete code that works :

    mycurve.h
    Qt Code:
    1. class MyCurve : public QwtPlotCurve
    2. {
    3. public:
    4. MyCurve(){}
    5. ~MyCurve(){}
    6.  
    7. virtual QList<QwtLegendData>legendData(void) const;
    8. };
    To copy to clipboard, switch view to plain text mode 

    mycurve.cpp
    Qt Code:
    1. QList<QwtLegendData> MyCurve::legendData(void) const
    2. {
    3. QList<QwtLegendData> data = QwtPlotCurve::legendData();
    4.  
    5. data[0].setValue(QwtLegendData::UserRole + 1, QVariant(this->pen().color()));
    6.  
    7. return data;
    8. }
    9.  
    10. ...MyCurve *curve = new MyCurve();
    11. ...
    12. curve->setPen(QColor(...,...,...);
    13. curve->legendData();
    14. ...
    15. curve->attach(this); ...
    16.  
    17. void Legend::updateItem( QStandardItem *item, const QwtLegendData &data )
    18. {
    19. ...
    20. const QVariant colorValue = data.value(QwtLegendData::UserRole + 1);
    21. QColor curveColor;
    22. if (colorValue.canConvert<QColor>())
    23. curveColor = colorValue.value<QColor>();
    24.  
    25. item->setBackground(QBrush(QColor(curveColor)));
    26. }
    To copy to clipboard, switch view to plain text mode 


Similar Threads

  1. Replies: 3
    Last Post: 3rd February 2014, 18:04
  2. Curve with more than one color?
    By sylphide in forum Qwt
    Replies: 1
    Last Post: 16th August 2011, 22:01
  3. Replies: 20
    Last Post: 22nd July 2010, 08:35
  4. Replies: 3
    Last Post: 22nd January 2010, 17:46
  5. Retrieve Curve values
    By nenukino in forum Qwt
    Replies: 2
    Last Post: 26th February 2008, 15:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.