Results 1 to 5 of 5

Thread: A suggestion about QwtPlotLegendItem

  1. #1
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default A suggestion about QwtPlotLegendItem

    Hi Uwe,
    I am using QWT6.1 - Downloaded at 20130630.
    In my application, it's required to refresh the legend item when the data is changed and I found that :

    In the updateLegend() of QwtPlotLegendItem, the layoutItems does not be cleared at each time.
    So the list length will be longer and longer, and not be equal to the count of actual items.
    eg.
    updateLegend( plotItem, dataOfSize1 );
    updateLegend( plotItem, dataOfSize2 );
    updateLegend( plotItem, dataOfSize1 );
    then at the third call of updateLegend(), it will crash.

    The codes are like below.

    Hope this information is useful.
    Nicho

    Qt Code:
    1. void QwtPlotLegendItem::updateLegend( const QwtPlotItem *plotItem,
    2. const QList<QwtLegendData> &data )
    3. {
    4. if ( plotItem == NULL )
    5. return;
    6.  
    7. QList<QwtLegendLayoutItem *> layoutItems;
    8.  
    9. QMap<const QwtPlotItem *, QList<QwtLegendLayoutItem *> >::iterator it =
    10. d_data->map.find( plotItem );
    11. if ( it != d_data->map.end() )
    12. layoutItems = it.value();
    13.  
    14. bool changed = false;
    15.  
    16. if ( data.size() != layoutItems.size() )
    17. {
    18. changed = true;
    19.  
    20. for ( int i = 0; i < layoutItems.size(); i++ )
    21. {
    22. d_data->layout->removeItem( layoutItems[i] );
    23. delete layoutItems[i];//app may crash here.
    24. }
    25. layoutItems.clear();//suggestion
    26. if ( it != d_data->map.end() )
    27. d_data->map.remove( plotItem );
    28.  
    29. if ( !data.isEmpty() )
    30. {
    31. for ( int i = 0; i < data.size(); i++ )
    32. {
    33. QwtLegendLayoutItem *layoutItem =
    34. new QwtLegendLayoutItem( this, plotItem );
    35. d_data->layout->addItem( layoutItem );
    36. layoutItems += layoutItem;
    37. }
    38.  
    39. d_data->map.insert( plotItem, layoutItems );
    40. }
    41. }
    42.  
    43. for ( int i = 0; i < data.size(); i++ )
    44. {
    45. if ( layoutItems[i]->data().values() != data[i].values() )
    46. {
    47. layoutItems[i]->setData( data[i] );
    48. changed = true;
    49. }
    50. }
    51.  
    52. if ( changed )
    53. {
    54. d_data->layout->invalidate();
    55. itemChanged();
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Nicho; 16th August 2013 at 13:12.

  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: A suggestion about QwtPlotLegendItem

    Your suggestion produces memory leaks avoiding a crash by not deleting the items. But obviously something is wrong with the layout items I can't see from your posting.

    Could you please upload a minimal compilable demo, where the problem happens ?

    Uwe

  3. #3
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: A suggestion about QwtPlotLegendItem

    Quote Originally Posted by Uwe View Post
    Your suggestion produces memory leaks avoiding a crash by not deleting the items. But obviously something is wrong with the layout items I can't see from your posting.

    Could you please upload a minimal compilable demo, where the problem happens ?

    Uwe

    Uwe,
    I am home now for the weekend.
    I will try to show you a demo on next Monday.
    I am not sure why Uwe think my suggestion will produce memory leak.
    What I mean is :
    1.keep the line below as it was. So the memory is released.
    Qt Code:
    1. delete legendItems[i ];//app may crash here
    To copy to clipboard, switch view to plain text mode 
    2.add the line below to make the list length back to be zero .
    Because after the delete, the pointers in the list are still there but illegal.
    Qt Code:
    1. legendItems.clear();//suggestion
    To copy to clipboard, switch view to plain text mode 

    Nicho

  4. #4
    Join Date
    Apr 2013
    Posts
    18
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: A suggestion about QwtPlotLegendItem

    Affirmative.
    When is planned to correct this error?

  5. #5
    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: A suggestion about QwtPlotLegendItem

    Got lost as there was never been the demo code. But anyway - bug is obvious and is fixed now in SVN ( all branches ).

    Uwe

Similar Threads

  1. QwtPlotLegendItem and QwtLegendItem
    By jesse_mark in forum Qwt
    Replies: 3
    Last Post: 14th November 2012, 08:10
  2. Newbie to QT, any suggestion?
    By xhsoldier in forum Qt Programming
    Replies: 2
    Last Post: 4th June 2009, 14:09
  3. Multicasting--Not able to Do it, suggestion pls
    By swamyonline in forum Qt Programming
    Replies: 2
    Last Post: 22nd January 2009, 01:45
  4. SVG suggestion
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 15:35

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.