Results 1 to 3 of 3

Thread: Sorting plot legend?

  1. #1
    Join Date
    Jan 2009
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sorting plot legend?

    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.
    Qt Code:
    1. class Legend : public QwtLegend
    2. {
    3. public:
    4. Legend(QWidget* pParent = 0) : QwtLegend(pParent)
    5. {
    6. }
    7.  
    8. QList<QWidget*> legendItems(void) const
    9. {
    10. QList<QWidget*> lLegendItems = QwtLegend::legendItems();
    11. qSort(lLegendItems.begin(), lLegendItems.end(), Legend::lessThan);
    12. return lLegendItems;
    13. }
    14.  
    15. static bool lessThan(QWidget* pLi1, QWidget* pLi2)
    16. {
    17. if(pLi1 && pLi2)
    18. {
    19. if (pLi1->inherits("QwtLegendItem") && pLi2->inherits("QwtLegendItem"))
    20. {
    21. QwtLegendItem* lLabel1 = (QwtLegendItem*)pLi1;
    22. QwtLegendItem* lLabel2 = (QwtLegendItem*)pLi2;
    23. return lLabel1->text().text().toLower() < lLabel2->text().text().toLower();
    24. }
    25. }
    26. return false;
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 
    The legend is added to the plot...
    Qt Code:
    1. Legend* lLegend = new Legend(mPlot->canvas());
    2. // some initialisations...
    3. 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

  2. #2
    Join Date
    Jan 2009
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting plot legend?

    The only way I found, was to create the curves and to add them to the plot already sorted alphabetically by title.

    Best Regards
    Stefan

  3. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting plot legend?

    The legend items are organized in a QLayout. So its order depends on the order they are inserted. If you want to reorder them you have to remove and reinsert them ( like you have found out yourself ).

    But instead of recreating your curves you could also detach/attach them - or best unset/set the QwtPlotItem::Legend flag.

    Uwe

Similar Threads

  1. Spectrogram Plot Hints
    By shargath in forum Qwt
    Replies: 2
    Last Post: 25th February 2009, 11:11
  2. Replies: 1
    Last Post: 24th February 2009, 18:35
  3. legend inside qwt plot
    By kaustav98255 in forum Qwt
    Replies: 5
    Last Post: 11th February 2009, 20:39
  4. Remove Legend items, move legends
    By giusepped in forum Qwt
    Replies: 0
    Last Post: 11th February 2009, 02:35
  5. Replies: 7
    Last Post: 22nd September 2008, 22:05

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.