Results 1 to 2 of 2

Thread: PyQwt - Toggling Legend Visibility

  1. #1

    Default PyQwt - Toggling Legend Visibility

    Greetings,

    I am hoping someone can help with this simple problem. I am trying to toggle the visibility of the legend of a plot but nothing seems to work.

    I have tried the following but nothing happens. The legend just remains visible:
    Qt Code:
    1. plot.legend().setVisible(False)/plot.legend().setVisible(True)
    2.  
    3. plot.legend().hide()/plot.legend().show()
    To copy to clipboard, switch view to plain text mode 

    I have tried:
    Qt Code:
    1. plot.legend().contentsWidget().setVisible(False)/plot.legend().contentsWidget().setVisible(True)
    To copy to clipboard, switch view to plain text mode 

    but this just hides the clickable legend items, the box of the legend is still visible which is not what I want. I would like the entire legend to appear or disappear.

    I have also tried keeping the legend around in my class after I create it by going:
    Qt Code:
    1. self.plotLegend=QwtLegend()
    2. #insert legend into plot here
    To copy to clipboard, switch view to plain text mode 
    Then, to make the legend “not visible” I remove it by calling
    Qt Code:
    1. plot.insertLegend(None)
    To copy to clipboard, switch view to plain text mode 

    but then when I try to reinsert my legend to make it “visible” again:
    Qt Code:
    1. plot.insertLegend(self.plotLegend, locationNotImportant)
    To copy to clipboard, switch view to plain text mode 
    I get an error stating the underlying C++ object has been deleted.

    Is there seriously no way to do this without creating a new legend every time? This would seem to be so simple…

    Any help is appreciated, thank you.

    (Using PyQwt 5.1.0)

  2. #2

    Default Re: PyQwt - Toggling Legend Visibility

    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:
    Qt Code:
    1. plot.plotLayout().setLegendPosition(QwtPlot.ExternalLegend)
    2. plot.legend().setVisible(False)
    3. 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):
    Qt Code:
    1. plot.plotLayout().setLegendPosition(QwtPlot.RightLegend)
    2. plot.legend().setVisible(True)
    3. 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):
    Qt Code:
    1. 00454 if ( d_data->legend &&
    2. 00455 d_data->layout->legendPosition() != ExternalLegend )
    3. 00456 {
    4. 00457 if (d_data->legend->itemCount() > 0)
    5. 00458 {
    6. 00459 d_data->legend->setGeometry(d_data->layout->legendRect());
    7. 00460 d_data->legend->show();
    8. 00461 }
    9. 00462 else
    10. 00463 d_data->legend->hide();
    11. 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.

Similar Threads

  1. Toggling the size of a window
    By Jeffb in forum Newbie
    Replies: 5
    Last Post: 10th August 2011, 02:50
  2. Problem building and installing PyQwt
    By di_zou in forum Qt Tools
    Replies: 0
    Last Post: 10th August 2010, 16:13
  3. Visibility of QDialog
    By elizabeth.h1 in forum Qt Programming
    Replies: 7
    Last Post: 15th October 2009, 18:40
  4. How to avoid keyboard-button-toggling ?
    By Comer352l in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 12:02
  5. Replies: 4
    Last Post: 23rd November 2006, 06:24

Tags for this Thread

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.