Results 1 to 7 of 7

Thread: Plotter class

  1. #1
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Plotter class

    Hello all,

    I'm running a data logger and I'm trying to implement a plotter that pops up when a button is pressed on my gui. I am aware of Qwt, but I don't need anything fancy and from what I've read it seems installing it is sort of a pain. Instead, I've been using a plotter class I found in a book I just bought, C++ GUI Programming with Qt 4. Right now the code is setup for plotting two horizontal lines, I'm just trying to get the code functional, I'll worry about getting it functional with my data logger later.

    I am getting no errors, nothing seems to happen when I click the button

    Here is some of the code;

    plotter.h
    Qt Code:
    1. #ifndef PLOTTER_H
    2. #define PLOTTER_H
    3.  
    4. #include <QMap>
    5. #include <QPixmap>
    6. #include <QVector>
    7. #include <QWidget>
    8.  
    9. class PlotSettings;
    10.  
    11. class Plotter : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Plotter (QWidget *parent = 0);
    17.  
    18. void setPlotSettings(const PlotSettings &settings);
    19. void setCurveData(int id, const QVector<QPointF> &data);
    20. void clearCurve(int id);
    21. QSize minimumSizeHint() const;
    22. QSize sizeHint() const;
    23.  
    24. public slots:
    25. void zoomIn();
    26. void zoomOut();
    27.  
    28. protected:
    29. void paintEvent(QPaintEvent *event);
    30. void resizeEvent(QResizeEvent *event);
    31. void mousePressEvent(QMouseEvent *event);
    32. void mouseMoveEvent(QMouseEvent *event);
    33. void mouseReleaseEvent(QMouseEvent *event);
    34. void keyPressEvent(QKeyEvent *event);
    35. void wheelEvent(QWheelEvent *event);
    36.  
    37. private:
    38. void updateRubberBandRegion();
    39. void refreshPixmap();
    40. void drawGrid(QPainter *painter);
    41. void drawCurves(QPainter *painter);
    42.  
    43. enum { Margin = 50 };
    44.  
    45. QToolButton *zoomInButton;
    46. QToolButton *zoomOutButton;
    47. QMap<int, QVector<QPointF> > curveMap;
    48. QVector<PlotSettings> zoomStack;
    49. int curZoom;
    50. bool rubberBandIsShown;
    51. QRect rubberBandRect;
    52. QPixmap pixmap;
    53. };
    54.  
    55. class PlotSettings
    56. {
    57. public:
    58. PlotSettings();
    59.  
    60. void scroll(int dx, int dy);
    61. void adjust();
    62. double spanX() const { return maxX - minX; }
    63. double spanY() const { return maxY - minY; }
    64.  
    65. double minX;
    66. double maxX;
    67. int numXTicks;
    68. double minY;
    69. double maxY;
    70. int numYTicks;
    71.  
    72. private:
    73. static void adjustAxis(double &min, double &max, int &numTicks);
    74. };
    75.  
    76. #endif
    To copy to clipboard, switch view to plain text mode 
    #include "plotter.h"

    plotSettings.cpp
    Qt Code:
    1. PlotSettings::PlotSettings()
    2. {
    3. minX = 0.0;
    4. maxX = 10.0;
    5. numTicks = 5;
    6.  
    7. minY = 0.0;
    8. maxY = 10.0;
    9. numYTicks = 5;
    10. }
    11.  
    12. //more code...
    To copy to clipboard, switch view to plain text mode 

    here is the implementation file, Plotter.cpp
    Qt Code:
    1. #include "plotter.h"
    2. #include <QtGui>
    3.  
    4. Plotter::Plotter(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. setBackgroundRole(QPalette::Dark);
    8. setAutoFillBackground(true);
    9. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    10. setFocusPolicy(Qt::StrongFocus);
    11. rubberBandIsShown = false;
    12.  
    13. zoomInButton = new QToolButton(this);
    14. zoomInButton->setIcon(QIcon(":/images/zoomin.png"));
    15. zoomInButton->adjustSize();
    16. connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn()));
    17.  
    18. zoomOutButton = new QToolButton(this);
    19. zoomOutButton->setIcon(QIcon(":/images/zoomout.png"));
    20. zoomOutButton->adjustSize();
    21. connect(zoomOutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
    22.  
    23. setPlotSettings(PlotSettings());
    24. }
    25.  
    26. //all the functions follow...
    To copy to clipboard, switch view to plain text mode 



    Now in my main window I call it

    Qt Code:
    1. //rest of program code
    2.  
    3. void mainWindow::on_pushButton_clicked()
    4. {
    5. PlotSettings settings;
    6. settings.minX(0.0);
    7. settings.maxX(100.0);
    8. settings.minY(0.0);
    9. settings.maxY(100.0);
    10.  
    11. int numPoints = 100;
    12. ArrayList<QPointF> points0 = new ArrayList<QPointF>();
    13. ArrayList<QPointF> points1 = new ArrayList<QPointF>();
    14. for (int x = 0; x < numPoints; ++x) {
    15. points0.add(new QPointF(x, Math.random() * 100));
    16. points1.add(new QPointF(x, Math.random() * 100));
    17. }
    18.  
    19. Plotter plotter;
    20. plotter.setWindowTitle(plotter.tr("Jambi Plotter"));
    21. plotter.setPlotSettings(settings);
    22. plotter.setCurveData(0, points0);
    23. plotter.setCurveData(1, points1);
    24. plotter.show();
    25.  
    26. //rest of program code
    27. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance for any help you can offer
    Last edited by awpitt13; 9th February 2012 at 00:31.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Plotter class

    See comments in the code:
    Qt Code:
    1. void mainWindow::on_pushButton_clicked()
    2. {
    3. PlotSettings settings;
    4. settings.minX(0.0); // not allowed; use assignment statement
    5. settings.maxX(100.0);
    6. settings.minY(0.0);
    7. settings.maxY(100.0);
    8.  
    9. int numPoints = 100;
    10. QVector<QPointF> points0; // use QVector - no need to create this on the heap
    11. ArrayList<QPointF> points1 = new ArrayList<QPointF>(); //same as above
    12. for (int x = 0; x < numPoints; ++x) {
    13. points0.append(QPointF(x, qrand() % 100)); // use QVector::append(), again no need to create the points on the heap
    14. // see qsrand & qrand()
    15. points1.add(new QPointF(x, Math.random() * 100)); //same as above
    16. }
    17.  
    18. Plotter plotter; // This does need to be created on the heap
    19. plotter.setWindowTitle(plotter.tr("Jambi Plotter"));
    20. plotter.setPlotSettings(settings);
    21. plotter.setCurveData(0, points0);
    22. plotter.setCurveData(1, points1);
    23. plotter.show();
    24. }
    To copy to clipboard, switch view to plain text mode 
    HTH
    Last edited by norobro; 9th February 2012 at 02:34. Reason: typo

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

    awpitt13 (9th February 2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotter class

    From your suggestions I have edited my code which can be seen below. When I click this button, nothing seems to happen. I feel like I need a .exec() command somewhere to pop up a new Ui but I'm not sure how to implement this...

    Qt Code:
    1. void mainWindow::on_pushButton_clicked()
    2. {
    3. PlotSettings settings;
    4. settings.minX = 0.0;
    5. settings.maxX = 100.0;
    6. settings.minY = 0.0;
    7. settings.maxY = 100.0;
    8.  
    9. int numPoints = 100;
    10. QVector<QPointF> points0;
    11. QVector<QPointF> points1;
    12. for (int x = 0; x < numPoints; ++x) {
    13. points0.append(QPointF(x, qrand() % 100));
    14.  
    15. points1.append(QPointF(x, qrand() % 100));
    16. }
    17.  
    18. Plotter plotter;
    19. plotter.setWindowTitle(plotter.tr("Plotter"));
    20. plotter.setPlotSettings(settings);
    21. plotter.setCurveData(0, points0);
    22. plotter.setCurveData(1, points1);
    23. plotter.show();
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Plotter class

    The one thing that you didn't do is create "plotter" on the heap at line 18:
    Qt Code:
    1. Plotter *plotter = new Plotter;
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to norobro for this useful post:

    awpitt13 (9th February 2012)

  7. #5
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotter class

    changing line 18 to
    Qt Code:
    1. Plotter *plotter = new Plotter;
    To copy to clipboard, switch view to plain text mode 

    results in the following errors:

    request for member 'setWindowTitle' in 'plotter' which is of non-class type 'Plotter*'
    request for member 'tr' in 'plotter' which is of non-class type 'Plotter*'
    request for member 'setPlotSettings' in 'plotter' which is of non-class type 'Plotter*'
    request for member 'setCurveData' in 'plotter' which is of non-class type 'Plotter*'
    request for member 'setCurveData' in 'plotter' which is of non-class type 'Plotter*'
    request for member 'show' in 'plotter' which is of non-class type 'Plotter*'

  8. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Plotter class

    You need to use the pointer member selection operator "->" to call each of those methods.

    e.g.:
    Qt Code:
    1. plotter->setWindowTitle(plotter->tr("Plotter"));
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to norobro for this useful post:

    awpitt13 (9th February 2012)

  10. #7
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotter class

    Thank you sir, it's working

Similar Threads

  1. Curve plotter widget, what's the best strategy?
    By Cruz in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2011, 17:03
  2. incremental 2d plotter with QGraphicsView
    By bingofuel in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2009, 06:03
  3. question about plotter
    By Cantora in forum Newbie
    Replies: 0
    Last Post: 6th April 2009, 10:45
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Plotter
    By chrisdsimpson in forum Newbie
    Replies: 4
    Last Post: 8th April 2008, 21:32

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.