Results 1 to 4 of 4

Thread: What is the elegant way to disable picker/zoomer when the plot is disabled?

  1. #1
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb What is the elegant way to disable picker/zoomer when the plot is disabled?

    I found that when the plot is disabled (by setEnabled(false) of plot's parent's parent), the picker and zoomer still works.
    What is the elegant way to disable picker&zoomer when the plot is disabled?


  2. #2
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: What is the elegant way to disable picker/zoomer when the plot is disabled?

    I would subclass QwtPlot and reimplement changeEvent()
    ...
    see code

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_zoomer.h>
    4. #include <QEvent>
    5.  
    6. class Plot: public QwtPlot
    7. {
    8. public:
    9. Plot(QWidget* parent=0):QwtPlot(parent)
    10. {
    11. _zoomer = new QwtPlotZoomer(this->canvas());
    12. }
    13. protected:
    14. void changeEvent(QEvent * event)
    15. {
    16. if(event->type()==QEvent::EnabledChange)
    17. {
    18. _zoomer->setEnabled(this->isEnabled());
    19. }
    20. }
    21. private:
    22. QwtPlotZoomer* _zoomer;
    23. };
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QApplication a(argc, argv);
    28. QwtPlot* plot = new Plot;
    29.  
    30. plot->setEnabled(false);
    31.  
    32. plot->show();
    33.  
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is the elegant way to disable picker/zoomer when the plot is disabled?

    Thanks your share.
    Because the zoomer/picker is not always enabled in my case,
    I must remember the picker/zoomer's state (named oldState) before the plot changes to disabled. Then the plot is enabled, the zoomer should be set to the oldState.

  4. #4
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: What is the elegant way to disable picker/zoomer when the plot is disabled?

    Because the zoomer/picker is not always enabled in my case,...
    Then change things up a little bit.

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_zoomer.h>
    4. #include <QEvent>
    5.  
    6. class Plot: public QwtPlot
    7. {
    8. public:
    9. Plot(QWidget* parent=0):QwtPlot(parent), _zoomer(0)
    10. {
    11.  
    12. }
    13. void setZoomer(QwtPlotZoomer* zoomer)
    14. {
    15. _zoomer = zoomer;
    16. }
    17. protected:
    18. void changeEvent(QEvent * event)
    19. {
    20. if(_zoomer && _zoomer->isEnabled() && event->type()==QEvent::EnabledChange)
    21. {
    22. _zoomer->setEnabled(this->isEnabled());
    23. }
    24. }
    25. private:
    26. QwtPlotZoomer* _zoomer;
    27.  
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication a(argc, argv);
    33.  
    34. Plot* plot = new Plot;
    35. QwtPlotZoomer* zoomer = new QwtPlotZoomer(plot->canvas());
    36. plot->setZoomer(zoomer);
    37.  
    38. //zoomer->setEnabled(false);
    39. plot->setEnabled(false);
    40.  
    41. plot->show();
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

    Because the zoomer/picker is not always enabled in my case,...
    Then change things up a little bit.

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_zoomer.h>
    4. #include <QEvent>
    5.  
    6. class Plot: public QwtPlot
    7. {
    8. public:
    9. Plot(QWidget* parent=0):QwtPlot(parent), _zoomer(0)
    10. {
    11.  
    12. }
    13. void setZoomer(QwtPlotZoomer* zoomer)
    14. {
    15. _zoomer = zoomer;
    16. }
    17. protected:
    18. void changeEvent(QEvent * event)
    19. {
    20. if(_zoomer && _zoomer->isEnabled() && event->type()==QEvent::EnabledChange)
    21. {
    22. _zoomer->setEnabled(this->isEnabled());
    23. }
    24. }
    25. private:
    26. QwtPlotZoomer* _zoomer;
    27.  
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication a(argc, argv);
    33.  
    34. Plot* plot = new Plot;
    35. QwtPlotZoomer* zoomer = new QwtPlotZoomer(plot->canvas());
    36. plot->setZoomer(zoomer);
    37.  
    38. //zoomer->setEnabled(false);
    39. plot->setEnabled(false);
    40.  
    41. plot->show();
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 29 minutes:


    The need to send setZoomer() to Plot coupled with the need to store instances as Plot* rather than QwtPlot* is troubling.
    Correct things by auto-detecting an instance of any Zoomer associated with the plot.

    When a Zoomer gets constructed, an event (QEvent) is generated. We make use of this fact in our auto-detect scheme.

    see code...
    note how the subclass, Plot, reimplements eventFilter() .

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_zoomer.h>
    4. #include <qwt_plot_canvas.h>
    5. #include <QEvent>
    6.  
    7. class Plot: public QwtPlot
    8. {
    9. public:
    10. Plot(QWidget* parent=0):QwtPlot(parent), _zoomer(0)
    11. {
    12.  
    13. }
    14. protected:
    15. bool eventFilter(QObject *obj, QEvent *ev)
    16. {
    17. if(!_zoomer)
    18. initZoomer();
    19. return QwtPlot::eventFilter(obj, ev);
    20. }
    21.  
    22. void changeEvent(QEvent * event)
    23. {
    24. if(_zoomer && _zoomer->isEnabled() && event->type()==QEvent::EnabledChange)
    25. {
    26. _zoomer->setEnabled(this->isEnabled());
    27. }
    28. }
    29. private:
    30. void initZoomer()
    31. {
    32. QList<QObject*> L = this->canvas()->findChildren<QObject*>();
    33. for(int i=0; i<L.size(); ++i)
    34. {
    35. _zoomer = dynamic_cast<QwtPlotZoomer*>(L[i]);
    36. if(_zoomer)
    37. break;
    38. }
    39. }
    40.  
    41. QwtPlotZoomer* _zoomer;
    42.  
    43. };
    44.  
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication a(argc, argv);
    48.  
    49. QwtPlot* plot = new Plot;
    50. QwtPlotZoomer* zoomer = new QwtPlotZoomer(plot->canvas());
    51. //zoomer->setEnabled(false);
    52. plot->setEnabled(false);
    53. plot->show();
    54.  
    55. return a.exec();
    56. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cah; 20th March 2014 at 10:13.

Similar Threads

  1. Qwt Plot Picker as position indicator
    By Kutuska in forum Qwt
    Replies: 1
    Last Post: 7th September 2012, 15:01
  2. Disable the QToolBar when the QMenuBar gets disabled?
    By dobedidoo in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 14:51
  3. Replies: 1
    Last Post: 24th February 2009, 19:35
  4. Plot Picker x=0
    By sun in forum Qwt
    Replies: 2
    Last Post: 7th October 2008, 08:43
  5. Replies: 0
    Last Post: 27th May 2008, 02:00

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.