Results 1 to 1 of 1

Thread: How to use curve fitting

  1. #1
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to use curve fitting

    Hi there,

    I'm drawing strokes with mouse movement. I'm keeping all points in a QVector<QPointF> or directly QPointF*, How can i apply curve fitting algorithm in my custom widget its own paintEvent. I tried somethings but i couldn't do that. Thanks ...

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QtGui>
    6.  
    7. class MyWidget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MyWidget(QWidget *parent = 0);
    12. virtual ~MyWidget();
    13.  
    14. void paintEvent(QPaintEvent *event);
    15.  
    16. virtual void mousePressEvent(QMouseEvent *event);
    17. virtual void mouseMoveEvent(QMouseEvent *event);
    18. virtual void mouseReleaseEvent(QMouseEvent *event);
    19.  
    20. private:
    21. QPointF m_start;
    22. QPointF m_end;
    23. QPolygonF m_points;
    24. QPolygonF m_fittedPoints;
    25.  
    26. bool m_isFitting;
    27.  
    28. };
    29.  
    30. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <qwt_spline.h>
    4. #include <qwt_curve_fitter.h>
    5.  
    6. QPolygonF interpolate(const QPolygonF& points, int numValues)
    7. {
    8. QwtSpline spline;
    9. QwtSplineCurveFitter curveFitter;
    10. bool pResult = true;
    11. Q_ASSERT(pResult != spline.setPoints(points));
    12. if ( !pResult ) {
    13. return points;
    14. }
    15.  
    16. // curveFitter.setSpline(spline);
    17. QPolygonF fitted = curveFitter.fitCurve(points);
    18.  
    19. return fitted;
    20. }
    21.  
    22. MyWidget::MyWidget(QWidget *parent) :
    23. QWidget(parent)
    24. {
    25. }
    26.  
    27. MyWidget::~MyWidget()
    28. {
    29. }
    30.  
    31. void MyWidget::mousePressEvent(QMouseEvent *event)
    32. {
    33. m_isFitting = false;
    34. m_start = event->pos();
    35. m_end = event->pos();
    36. }
    37.  
    38. void MyWidget::mouseMoveEvent(QMouseEvent *event)
    39. {
    40. m_start = m_end;
    41. m_end = event->pos();
    42.  
    43. // m_points.append(m_start);
    44. // m_points.append(m_end);
    45. m_points << m_start << m_end;
    46. update();
    47. }
    48.  
    49. void MyWidget::mouseReleaseEvent(QMouseEvent *event)
    50. {
    51. // I want to apply curve fitting to m_points here
    52. m_fittedPoints = interpolate(m_points, m_points.size());
    53. // m_fittedPoints is equal to fitted points
    54. m_isFitting = true;
    55. update();
    56. }
    57.  
    58. void MyWidget::paintEvent(QPaintEvent *event)
    59. {
    60.  
    61. QPainter painter(this);
    62. painter.setPen(Qt::SolidLine);
    63.  
    64. if (!m_isFitting)
    65. painter.drawLines(m_points.data(), m_points.size()/2);
    66. else
    67. painter.drawLines(m_fittedPoints.data(), m_fittedPoints.size()/2);
    68. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nightroad; 30th June 2011 at 14:59.

Similar Threads

  1. Curve Fitting Not Always Applied
    By Mannion in forum Qwt
    Replies: 3
    Last Post: 16th March 2011, 08:11
  2. Fitting QWebView to content
    By Sölve in forum Newbie
    Replies: 0
    Last Post: 7th September 2010, 00:25
  3. Replies: 4
    Last Post: 29th April 2010, 06:11
  4. Moving QGraphicsItems and Fitting QGraphicsView
    By aladagemre in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2010, 18:21
  5. Replies: 1
    Last Post: 22nd January 2010, 14:34

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.