In case anyone is actually interested (and because I myself hate seeing questions with no answer or follow-up), I figured out a hack to do this. When the user wishes to hide the legend, I change the position of the legend to External:
plot.
plotLayout().
setLegendPosition(QwtPlot.
ExternalLegend)plot.legend().setVisible(False)
plot.updateLayout()
plot.plotLayout().setLegendPosition(QwtPlot.ExternalLegend)
plot.legend().setVisible(False)
plot.updateLayout()
To copy to clipboard, switch view to plain text mode
The call to setVisible() now successfully hides the legend. Then when the user wishes it to reappear, I change the position back to where it was before (in my case, the right side):
plot.
plotLayout().
setLegendPosition(QwtPlot.
RightLegend)plot.legend().setVisible(True)
plot.updateLayout()
plot.plotLayout().setLegendPosition(QwtPlot.RightLegend)
plot.legend().setVisible(True)
plot.updateLayout()
To copy to clipboard, switch view to plain text mode
The legend then reappears thereby retaining the “pressed status†of its items. I came to this solution by going through the Qwt source and noticed something in the updateLayout() method of QwtPlot (and somewhere else as well, though the location eludes me at the moment):
00454 if ( d_data->legend &&
00455 d_data->layout->legendPosition() != ExternalLegend )
00456 {
00457 if (d_data->legend->itemCount() > 0)
00458 {
00459 d_data->legend->setGeometry(d_data->layout->legendRect());
00460 d_data->legend->show();
00461 }
00462 else
00463 d_data->legend->hide();
00464 }
00454 if ( d_data->legend &&
00455 d_data->layout->legendPosition() != ExternalLegend )
00456 {
00457 if (d_data->legend->itemCount() > 0)
00458 {
00459 d_data->legend->setGeometry(d_data->layout->legendRect());
00460 d_data->legend->show();
00461 }
00462 else
00463 d_data->legend->hide();
00464 }
To copy to clipboard, switch view to plain text mode
This code results in the legend always being shown as long as there are items in it no matter what. So no matter how much you try to hide the legend, this will override it and make the legend appear whenever the layout of the plot gets updated. I then noticed that it doesn’t do this when the legend is set as External, which led me to my above solution.
I hope this helps others.
Bookmarks