Results 1 to 2 of 2

Thread: Make QwtPlotRescaler reflect data changes

  1. #1
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Make QwtPlotRescaler reflect data changes

    I need a plot to maintain a 1:1 aspect ratio, but I'll occasionally change the data. When I try this, the axes don't appear to be altered to fit the new range of the data. For example, when I initially plot a long line (going from (0,0) to (20,20), say), I get the expected behavior; the plot maintains the 1:1 aspect ratio when resizing. But if I then setData() to a shorter line (say, (0,0) to (10,10)), the axes remain the same, and there's a lot of unused space in the plotting area. How do I get the axes to readjust to the new range of the data and keep a 1:1 aspect ratio?

    I'm using Qt 4.6.2, Qwt 5.2.1, and GCC 4.5.0 on CentOS 4.

    A crude example follows. main.h:
    Qt Code:
    1. #include <QWidget>
    2.  
    3.  
    4. class QwtPlot;
    5. class QwtPlotRescaler;
    6.  
    7.  
    8. class Window : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Window( QWidget* parent = 0 );
    14.  
    15. private:
    16. QwtPlot* plot;
    17. QwtPlotCurve* curve;
    18. QwtPlotRescaler* rescaler;
    19. QPushButton* switchDataButton;
    20. QHBoxLayout* layout;
    21.  
    22. private slots:
    23. void switchData();
    24. };
    To copy to clipboard, switch view to plain text mode 
    main.cpp:
    Qt Code:
    1. #include "main.h"
    2. #include "qwt_plot.h"
    3. #include "qwt_plot_curve.h"
    4. #include "qwt_plot_rescaler.h"
    5. #include <QtGui>
    6.  
    7.  
    8. const double HI = 20.0;
    9. const double LO = 10.0;
    10. const int N = 5;
    11. const double DATA_HI[] = { 0.0, 0.25*HI, 0.5*HI, 0.75*HI, HI };
    12. const double DATA_LO[] = { 0.0, 0.25*LO, 0.5*LO, 0.75*LO, LO };
    13.  
    14.  
    15. Window::Window( QWidget* parent ) : QWidget( parent )
    16. {
    17. resize( 400, 500 );
    18.  
    19. plot = new QwtPlot( this );
    20. curve = new QwtPlotCurve;
    21. curve->setData( DATA_HI, DATA_HI, N );
    22. curve->attach( plot );
    23. rescaler = new QwtPlotRescaler( plot->canvas() );
    24. rescaler->setRescalePolicy( QwtPlotRescaler::Fitting );
    25.  
    26. switchDataButton = new QPushButton( "Switch data", this );
    27. connect( switchDataButton, SIGNAL( clicked() ),
    28. this, SLOT( switchData() ) );
    29.  
    30. layout = new QHBoxLayout;
    31. layout->addWidget( plot );
    32. layout->addWidget( switchDataButton );
    33. setLayout( layout );
    34.  
    35. plot->replot();
    36. }
    37.  
    38.  
    39. void Window::switchData()
    40. {
    41. if ( curve->maxYValue() > LO ) curve->setData( DATA_LO, DATA_LO, N );
    42. else curve->setData( DATA_HI, DATA_HI, N );
    43. plot->updateAxes(); // This doesn't make a difference that I can tell
    44. plot->replot();
    45. rescaler->rescale();
    46. }
    47.  
    48.  
    49. int main( int argc, char* argv[] )
    50. {
    51. QApplication app( argc, argv );
    52.  
    53. Window window;
    54. window.show();
    55.  
    56. return app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Make QwtPlotRescaler reflect data changes

    The rescaler implicitely calls setAxisScale(), what disables autoscaling. Guess if you reenable autoscaling before calling updateAxes you probably have what you want:

    Qt Code:
    1. void Window::switchData()
    2. {
    3. if ( curve->maxYValue() > LO ) curve->setData( DATA_LO, DATA_LO, N );
    4. else curve->setData( DATA_HI, DATA_HI, N );
    5.  
    6. for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    7. plot->setAxisAutoScale(axis);
    8.  
    9. plot->updateAxes();
    10. rescaler->rescale();
    11. }
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

  3. The following user says thank you to Uwe for this useful post:

    hector1984 (26th August 2010)

Similar Threads

  1. How to make QAbstractItemModel 's data checkable
    By nifei in forum Qt Programming
    Replies: 12
    Last Post: 1st April 2013, 19:52
  2. Replies: 7
    Last Post: 25th November 2009, 12:10
  3. qwtconfig.pri changes dont reflect in make for ARM
    By gautammorey in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th November 2009, 08:58
  4. Replies: 0
    Last Post: 19th November 2009, 04:48
  5. How to reflect QGraphicsItem?
    By Radagast in forum Qt Programming
    Replies: 15
    Last Post: 26th June 2008, 22:52

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.