Results 1 to 14 of 14

Thread: Draw a line animation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draw a line animation

    The code looks fine (more or less). The problem has to be elsewhere (e.g. the event loop not working).
    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.


  2. #2
    Join Date
    Jul 2013
    Location
    London
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Draw a line animation

    I think its the loop too but it was only thing i can think of, any suggestion how I can fix them or do it differently ?

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

    Default Re: Draw a line animation

    Sure. Declare a property (of type QPointF) in your dialog class that will control the end point of the line. Then use QPropertyAnimation to animate that property.

    Qt Code:
    1. class Dialog : public QDialog {
    2. Q_OBJECT
    3. Q_PROPERTY(QPointF endPoint READ endPoint WRITE setEndPoint)
    4. public:
    5. // ...
    6. QPointF endPoint() const { ... }
    7. public slots:
    8. void setEndPoint(const QPointF &pt) {
    9. QLineF l = line->line();
    10. l.setP2(pt);
    11. line->setLine(l);
    12. }
    13. };
    14.  
    15. // ...
    16.  
    17. QPropertyAnimation *anim = new QPropertyAnimation(&dialog, "endPoint");
    18. anim->setStartValue(...);
    19. anim->setEndValue(...);
    20. anim->setDuration(...);
    21. anim->start();
    To copy to clipboard, switch view to plain text mode 
    Remember to run the event loop.
    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.


  4. #4
    Join Date
    Jul 2013
    Location
    London
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Draw a line animation

    I'VE DONE IT! I think what was not needed here is subclassing QGraphicsLineItem so I removed it then just add simple QGraphicsLineItem to the scene and using your above function to edit the endPoint and it worked. Much simpler, maybe I made it more complex then needed.

    Thanks very much for you patience with me on this problem, I've learnt alot.

    I've attached a working version in case someone ever need it.
    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsView>
    7. #include <QTimer>
    8. #include <QGraphicsLineItem>
    9.  
    10.  
    11.  
    12. namespace Ui {
    13. class Dialog;
    14. }
    15.  
    16. class Dialog : public QDialog
    17. {
    18. Q_OBJECT
    19. Q_PROPERTY(QPointF endPoint READ endPoint WRITE setEndPoint)
    20.  
    21. public:
    22. explicit Dialog(QWidget *parent = 0);
    23. ~Dialog();
    24. QPointF endPoint() const;
    25. public slots:
    26. QPointF setEndPoint(const QPointF &pt);
    27. private:
    28. Ui::Dialog *ui;
    29. QTimer *timer;
    30. };
    31.  
    32. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4.  
    5. #include <QGraphicsView>
    6. #include <QPropertyAnimation>
    7.  
    8. Dialog::Dialog(QWidget *parent) :
    9. QDialog(parent),
    10. ui(new Ui::Dialog)
    11. {
    12. ui->setupUi(this);
    13.  
    14. scene = new QGraphicsScene(this);
    15.  
    16. ui->graphicsView->setScene(scene);
    17. ui->graphicsView->setSceneRect(0,0,300,300);
    18. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    19.  
    20. myLine = new QGraphicsLineItem(0,0,10,10);
    21. myLine->setPen(QPen(Qt::red, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    22.  
    23.  
    24. scene->addItem(myLine);
    25.  
    26. QPropertyAnimation *anim = new QPropertyAnimation(this, "endPoint");
    27. anim->setStartValue(QPointF(50,50));
    28. anim->setEndValue(QPointF(200,200));
    29. anim->setDuration(1000);
    30. anim->start();
    31.  
    32.  
    33. }
    34.  
    35. Dialog::~Dialog()
    36. {
    37. delete ui;
    38. }
    39.  
    40. QPointF Dialog::endPoint() const
    41. {
    42. QLineF l = myLine->line();
    43. return l.p2();
    44. }
    45.  
    46. QPointF Dialog::setEndPoint(const QPointF &pt)
    47. {
    48. QLineF l = myLine->line();
    49. l.setP2(pt);
    50. myLine->setLine(l);
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ZsCosa; 30th July 2013 at 15:16.

Similar Threads

  1. To draw a line
    By vinayaka in forum Qt Quick
    Replies: 1
    Last Post: 6th June 2011, 10:53
  2. Draw Line
    By sagirahmed in forum Newbie
    Replies: 5
    Last Post: 18th October 2010, 07:49
  3. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  4. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02
  5. Best way to draw a Line
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 09: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
  •  
Qt is a trademark of The Qt Company.