Results 1 to 9 of 9

Thread: Plotting points

  1. #1
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Plotting points

    Hi everyone,

    Where can I get a library, or application, where a user can click on the screen some points, and then write to a file the points that the user picked. I would like to integrate this in an application I'm building.?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Plotting points

    I suggest you implement your own widget for that. It's about 20 minutes of work.

  3. #3
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Plotting points

    It's about 20 minutes of work.
    For someone that knows what they're doing

    Anyway, any good tutorials on that, or what QWidget could I use as a basis for this plotting widget?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Plotting points

    No, really it is 20 minutes of work

    QWidget is the obvious candidate for the base class. You might also search the forum, I remember implementing something like that as an example snippet, but it was few months ago, so it might not be easy to find it.

    Basically you need to subclass QWidget and reimplement its paintEvent() for drawing and mouseReleaseEvent() for clicking. In the mouse event store coordinates of points you click on in some list (QVector<QPoint> is probably a good candidate) and schedule an update of the widget where you will repaint all the points. As for saving the result to a file, use QFile and optionally QDataStream or QTextStream.

    Here is a rough implementation of the widget (not tested):

    Qt Code:
    1. class Widget : public QWidget {
    2. Q_OBJECT
    3. Q_PROPERTY(int count READ count)
    4. Q_PROPERTY(QVector<QPoint> points READ points WRITE setPoints)
    5. public:
    6. Widget(QWidget *parent = 0) : QWidget(parent){}
    7. const QVector<QPoint> &points() { return m_points; } // for saving
    8. void setPoints(const QVector<QPoint> &pts) { m_points = pts; update(); } // for loading
    9. const QPoint &point(int i) const { return m_points.at(i); }
    10. void setPoint(int i, const QPoint &pt) { m_points[i] = pt; update(); }
    11. int count() const { return m_points.count(); }
    12. public slots:
    13. void clear() { m_points.clear(); update(); }
    14. protected:
    15. void paintEvent(QPaintEvent *pe){
    16. QPainter painter(this);
    17. painter->drawPoints(m_points.constData(), m_points.size());
    18. }
    19. void mouseReleaseEvent(QMouseReleaseEvent *me){
    20. m_points << me->pos();
    21. update();
    22. }
    23. private:
    24. QVector<QPoint> m_points;
    25. };
    To copy to clipboard, switch view to plain text mode 

    There... 7 minutes 13 more to serialize and deserialize the points to a file

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Plotting points

    No, really it is 20 minutes of work

    QWidget is the obvious candidate for the base class. You might also search the forum, I remember implementing something like that as an example snippet, but it was few months ago, so it might not be easy to find it.

    Basically you need to subclass QWidget and reimplement its paintEvent() for drawing and mouseReleaseEvent() for clicking. In the mouse event store coordinates of points you click on in some list (QVector<QPoint> is probably a good candidate) and schedule an update of the widget where you will repaint all the points. As for saving the result to a file, use QFile and optionally QDataStream or QTextStream.

    Here is a rough implementation of the widget (not tested):

    Qt Code:
    1. class Widget : public QWidget {
    2. Q_OBJECT
    3. Q_PROPERTY(int count READ count)
    4. Q_PROPERTY(QVector<QPoint> points READ points WRITE setPoints)
    5. public:
    6. Widget(QWidget *parent = 0) : QWidget(parent){}
    7. const QVector<QPoint> &points() { return m_points; } // for saving
    8. void setPoints(const QVector<QPoint> &pts) { m_points = pts; update(); } // for loading
    9. const QPoint &point(int i) const { return m_points.at(i); }
    10. void setPoint(int i, const QPoint &pt) { m_points[i] = pt; update(); }
    11. int count() const { return m_points.count(); }
    12. public slots:
    13. void clear() { m_points.clear(); update(); }
    14. protected:
    15. void paintEvent(QPaintEvent *pe){
    16. QPainter painter(this);
    17. painter->drawPoints(m_points.constData(), m_points.size());
    18. }
    19. void mouseReleaseEvent(QMouseReleaseEvent *me){
    20. m_points << me->pos();
    21. update();
    22. }
    23. private:
    24. QVector<QPoint> m_points;
    25. };
    To copy to clipboard, switch view to plain text mode 

    There... 7 minutes 13 more left to serialize and deserialize the points to a file

  6. #6
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Plotting points

    Hmmm I don't get it. I have this 2 files:

    widget.h
    Qt Code:
    1. #include <QtGui>
    2. #include <QVector>
    3. #include <QPoint>
    4. #include <QPainter>
    5. #include <QPaintDevice>
    6. #include <QEvent>
    7. #include <QCursor>
    8.  
    9. class Widget : public QWidget {
    10. Q_OBJECT
    11. Q_PROPERTY(int count READ count)
    12. Q_PROPERTY(QVector<QPoint> points READ points WRITE setPoints)
    13. public:
    14. Widget(QWidget *parent = 0) : QWidget(parent){}
    15. const QVector<QPoint> &points() { return m_points;} // for saving
    16. void setPoints(const QVector<QPoint> &pts) { m_points = pts; update();} // for loading
    17. const QPoint &point(int i) const { return m_points.at(i);}
    18. void setPoint(int i, const QPoint &pt) { m_points[i] = pt; update();}
    19. int count() const { return m_points.count();}
    20. public slots:
    21. void clear() { m_points.clear(); update();}
    22. protected:
    23. void paintEvent(QPaintEvent *pe){
    24. QPainter painter(this);
    25. painter.drawPoints(m_points.constData(), m_points.size());
    26. }
    27. void mouseReleaseEvent(QEvent *e){
    28. if ( e->type() == QEvent::MouseButtonRelease ) {
    29. m_points << QCursor::pos();
    30. update();
    31. }
    32. }
    33. private:
    34. QVector<QPoint> m_points;
    35. };
    To copy to clipboard, switch view to plain text mode 

    and

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QHBoxLayout>
    3. #include "widget.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget *window = new QWidget;
    9.  
    10. window->setWindowTitle("Blah");
    11.  
    12. Widget *wid = new Widget();
    13.  
    14.  
    15. QHBoxLayout *layout = new QHBoxLayout;
    16. layout->addWidget(wid);
    17.  
    18. window->show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    It's not painting though :\

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Plotting points

    Compare my mouseReleaseEvent and yours. Mine is incorrect but is closer to being correct than yours
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Plotting points

    Aghh I don't get it :\

    I mean, don't I need an if stmt to verify what type of event it was.

    I tried this:
    Qt Code:
    1. void mouseReleaseEvent(QMouseEvent *e){
    2. if ( e->type() == QMouseEvent::MouseButtonRelease ) {
    3. m_points << QCursor::pos();
    4. update();
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I mean this is how Interpreting this: Maybe I've got something wrong.

    1. You click.
    2. mouseReleaseEvent() is called, it checks what type of MouseEvent it was if it is a release then it goes in.
    3. point is added to the list of points.
    4. update() is called which in turn calls paintEvent( )... I'm guessing.

    Yet it still doesn't paint.. so there must be a flaw in my logic

    btw the code I showed above is all I have, I shouldn't need more code to paint points on the widget, do I? O_o.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Plotting points

    My implementation is correct and sufficient. It's just it should say QMouseEvent and not QMouseReleaseEvent in the signature.

    In mouseReleaseEvent you can't get other event than a mouse release, that's why it's called "mouseReleaseEvent" and not "someGenericMouseEvent", there is no point in checking the event type. QCursor::pos() returns a global cursor position (relative to the origin of the screen) whereas QMouseEvent::pos() returns a local position (relative to the widget origin) and as you want to paint on the widget, you need coordinates relative to the widget and not the screen.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 2nd October 2008, 07:29
  2. Fast plotting widget
    By nemesis in forum Qt Programming
    Replies: 0
    Last Post: 30th June 2008, 10:12
  3. Plotting from Left to Right ??
    By dheeraj in forum Qwt
    Replies: 3
    Last Post: 3rd June 2008, 07:09
  4. Multi curves plotting issue
    By fallen in forum Qwt
    Replies: 5
    Last Post: 6th May 2008, 05:47
  5. OpenGL control points
    By jhearon in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 18: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.