Results 1 to 3 of 3

Thread: Problem with scales

  1. #1
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Problem with scales

    In below code I'm trying to set scales for X and Y axis. I use setAxisScale but scale doesn't change and I don't see any curves. I must admit Qwt doesn't have user-friendly designed interface.

    Qt Code:
    1. #ifndef PLOT_H
    2. #define PLOT_H
    3.  
    4. #include <qvaluelist.h>
    5.  
    6. #include <qwt_plot.h>
    7. #include <qwt_plot_marker.h>
    8. #include <qwt_plot_curve.h>
    9. #include <qwt_legend.h>
    10. #include <qwt_scale_engine.h>
    11. #include <qwt_data.h>
    12. #include <qwt_text.h>
    13.  
    14. #include <math.h>
    15.  
    16. class Plot : public QwtPlot
    17. {
    18. public:
    19. Plot()
    20. {
    21. curves.setAutoDelete(true);
    22. X.setAutoDelete(true);
    23. Y.setAutoDelete(true);
    24.  
    25. setTitle("A Simple QwtPlot Demonstration");
    26. insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    27.  
    28. // Set axis titles
    29. setAxisTitle(xBottom, "x -->");
    30. setAxisTitle(yLeft, "y -->");
    31. /*
    32. setAxisAutoScale(xBottom);
    33. setAxisAutoScale(xTop);
    34. setAxisAutoScale(yLeft);
    35. setAxisAutoScale(yRight);
    36. */
    37. minX = 0.0;
    38. maxX = 0.0;
    39. minY = 0.0;
    40. maxY = 0.0;
    41.  
    42. setAxisScaleEngine(xBottom, new QwtLinearScaleEngine());
    43. setAxisScaleEngine(yLeft, new QwtLinearScaleEngine());
    44.  
    45. // Insert markers
    46.  
    47. // ...a horizontal line at y = 0...
    48. /*
    49. QwtPlotMarker *mY = new QwtPlotMarker();
    50. mY->setLabel(QString::fromLatin1("y = 0"));
    51. mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    52. mY->setLineStyle(QwtPlotMarker::HLine);
    53. mY->setYValue(0.0);
    54. mY->attach(this);
    55. */
    56.  
    57. // ...a vertical line at x = 2 * pi
    58. /*
    59. QwtPlotMarker *mX = new QwtPlotMarker();
    60. mX->setLabel(QString::fromLatin1("x = 2 pi"));
    61. mX->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    62. mX->setLineStyle(QwtPlotMarker::VLine);
    63. mX->setXValue(1000.0);
    64. mX->attach(this);
    65. */
    66. }
    67.  
    68. ~Plot()
    69. {
    70. curves.clear();
    71. X.clear();
    72. Y.clear();
    73. }
    74.  
    75. void addCurve(QValueList<double> & list)
    76. {
    77. double * x = new double[list.count()];
    78. double * y = new double[list.count()];
    79.  
    80. for (unsigned int i = 0 ; i < list.count() ; i++)
    81. {
    82. x[i] = i;
    83. y[i] = list[i];
    84. }
    85.  
    86. X.append(x);
    87. Y.append(y);
    88.  
    89.  
    90. c->setRawData(x, y, list.count());
    91. c->attach(this);
    92.  
    93. if (c->minXValue() < minX)
    94. minX = c->minXValue();
    95.  
    96. if (c->maxXValue() > maxX)
    97. maxX = c->maxXValue();
    98.  
    99. if (c->minYValue() < minY)
    100. minY = c->minYValue();
    101.  
    102. if (c->maxYValue() > maxY)
    103. maxY = c->maxYValue();
    104.  
    105. setAxisScale(xBottom, minX, maxX);
    106. setAxisScale(yLeft, minY, maxY);
    107.  
    108. curves.append(c);
    109. }
    110.  
    111. private:
    112. QPtrList<QwtPlotCurve> curves;
    113. QPtrList<double> X;
    114. QPtrList<double> Y;
    115. double minX;
    116. double maxX;
    117. double minY;
    118. double maxY;
    119. };
    120.  
    121. #endif
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Problem with scales

    a) Guess you miss to call replot ( or enable autoReplot() ).
    b) Adopting the scales to the bounding rect of your curves is what autoscaling does. So better remove your redundant code.
    c) Using setRawData is intended to avoid pointless copying of values. Copying + using setRawData doesn't make much sense because you have to take care of the temporary memory (better copy your values into a QwtArray<double> ). But in your example I would implement an individual data class to avoid wasting memory for the pointless x array.

    Qt Code:
    1. #ifndef PLOT_H
    2. #define PLOT_H
    3.  
    4. #include <qwt_plot.h>
    5. #include <qwt_plot_curve.h>
    6. #include <qwt_legend.h>
    7. #include <qwt_data.h>
    8.  
    9. class YourData: public QwtData
    10. {
    11. public:
    12. YourData(const QValueList<double>& values):
    13. _values(values)
    14. {
    15. }
    16.  
    17. virtual QwtData *copy() const
    18. {
    19. return new YourData(_values);
    20. }
    21.  
    22. virtual size_t size() const
    23. {
    24. return _values.count();
    25. }
    26.  
    27. virtual double x(size_t i) const
    28. {
    29. return i;
    30. }
    31.  
    32. virtual double y(size_t i) const
    33. {
    34. return _values[i];
    35. }
    36. private:
    37. QValueList<double> _values;
    38. };
    39.  
    40.  
    41. class Plot : public QwtPlot
    42. {
    43. public:
    44. Plot()
    45. {
    46. setTitle("A Simple QwtPlot Demonstration");
    47. insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    48.  
    49. // Set axis titles
    50. setAxisTitle(xBottom, "x -->");
    51. setAxisTitle(yLeft, "y -->");
    52. }
    53.  
    54. void addCurve(QValueList<double> & list)
    55. {
    56. c->setData(YourData(list));
    57. c->attach(this);
    58.  
    59. replot();
    60. }
    61. };
    To copy to clipboard, switch view to plain text mode 

    Uwe

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

    fear (5th May 2008)

  4. #3
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem with scales

    Yes it was because of no replot(). I also used idea with setData(). Thanks.

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.