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
void QwtPlotLegendItem
::updateLegend( const QwtPlotItem *plotItem,
const QList<QwtLegendData> &data )
{
if ( plotItem == NULL )
return;
QList<QwtLegendLayoutItem *> layoutItems;
QMap<const
QwtPlotItem *, QList<QwtLegendLayoutItem
*> >
::iterator it
= d_data->map.find( plotItem );
if ( it != d_data->map.end() )
layoutItems = it.value();
bool changed = false;
if ( data.size() != layoutItems.size() )
{
changed = true;
for ( int i = 0; i < layoutItems.size(); i++ )
{
d_data->layout->removeItem( layoutItems[i] );
delete layoutItems[i];//app may crash here.
}
layoutItems.clear();//suggestion
if ( it != d_data->map.end() )
d_data->map.remove( plotItem );
if ( !data.isEmpty() )
{
for ( int i = 0; i < data.size(); i++ )
{
QwtLegendLayoutItem *layoutItem =
new QwtLegendLayoutItem( this, plotItem );
d_data->layout->addItem( layoutItem );
layoutItems += layoutItem;
}
d_data->map.insert( plotItem, layoutItems );
}
}
for ( int i = 0; i < data.size(); i++ )
{
if ( layoutItems[i]->data().values() != data[i].values() )
{
layoutItems[i]->setData( data[i] );
changed = true;
}
}
if ( changed )
{
d_data->layout->invalidate();
itemChanged();
}
}
void QwtPlotLegendItem::updateLegend( const QwtPlotItem *plotItem,
const QList<QwtLegendData> &data )
{
if ( plotItem == NULL )
return;
QList<QwtLegendLayoutItem *> layoutItems;
QMap<const QwtPlotItem *, QList<QwtLegendLayoutItem *> >::iterator it =
d_data->map.find( plotItem );
if ( it != d_data->map.end() )
layoutItems = it.value();
bool changed = false;
if ( data.size() != layoutItems.size() )
{
changed = true;
for ( int i = 0; i < layoutItems.size(); i++ )
{
d_data->layout->removeItem( layoutItems[i] );
delete layoutItems[i];//app may crash here.
}
layoutItems.clear();//suggestion
if ( it != d_data->map.end() )
d_data->map.remove( plotItem );
if ( !data.isEmpty() )
{
for ( int i = 0; i < data.size(); i++ )
{
QwtLegendLayoutItem *layoutItem =
new QwtLegendLayoutItem( this, plotItem );
d_data->layout->addItem( layoutItem );
layoutItems += layoutItem;
}
d_data->map.insert( plotItem, layoutItems );
}
}
for ( int i = 0; i < data.size(); i++ )
{
if ( layoutItems[i]->data().values() != data[i].values() )
{
layoutItems[i]->setData( data[i] );
changed = true;
}
}
if ( changed )
{
d_data->layout->invalidate();
itemChanged();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks