Results 1 to 14 of 14

Thread: Draw a line animation

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

    Default Draw a line animation


    Hi, I'm trying to achieve a drawing animation similar to the above. Can anyone suggest me a direction on this matter? Should I use a QTimer or should I use QPropertyAnimation and changing QLine properties. Thanks very much!

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

    Default Re: Draw a line animation

    Either will work.
    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.


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

    ZsCosa (27th July 2013)

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

    Default Re: Draw a line animation

    Thanks alot for your reply. Do you think this could have been done using QGraphicsItemAnimation, I've been reading the documentation but can't find any properties that can support that. Or I've missed something, would really appreciate if you can give me a hint.

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

    Default Re: Draw a line animation

    It could have but I don't see the point of looking for more solutions if you already have two to choose from.
    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.


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

    Default Re: Draw a line animation

    Actually I spent quite sometime to try the QGraphicsItemAnimation first, after failing I'm looking for more solutions. It still bugging me so it will be awesome if you can hint me with that so I can get it out of my head . Thanks anyway!

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

    Default Re: Draw a line animation

    If you ask a specific question then I will answer it. All three of the solutions you found yourself have examples in the documentation on how to use them. QGraphicsItemAnimation is probably the worst approach you might have taken though since in your case it requires subclassing QGraphicsItemAnimation and reimplementing one of its virtual methods.
    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. #7
    Join Date
    Jul 2013
    Location
    London
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Draw a line animation

    Hi, I wanted to spent more time then come back with some questions so here I am.

    I try with the QTimer approach so I draw a Line
    Qt Code:
    1. void myitems::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QPointF point1(0,0);
    4. QPointF point2(100,100);
    5. painter->setRenderHint(QPainter::Antialiasing);
    6. painter->setPen(QPen(Qt::red, 8, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
    7. painter->setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
    8. painter->drawLine(point1,point2);
    9. }
    To copy to clipboard, switch view to plain text mode 
    And create a timer:
    Qt Code:
    1. timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), scene, SLOT(advance()));
    3. timer->start(100);
    To copy to clipboard, switch view to plain text mode 

    Connect timeout() with advance() slot, what should I put in this advance() to make the line draw, I've tried something but I was a mess.
    Qt Code:
    1. void myitems::advance(int phase)
    2. {
    3. if(!phase) return;
    4.  
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    Thank you.

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

    Default Re: Draw a line animation

    If you wish to use advance(), put there some code that will change one of the end points of the line. Remember that you can only draw within the item's bounding rect so you'll need to update that as well. However the simplest approach would be to connect the timer to some slot and in the slot use QGraphicsLineItem::setLine() to update the line. There are more sophisticated solutions available however I suggest to avoid them if you don't feel comfortable with Qt yet.
    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.


  10. The following user says thank you to wysota for this useful post:

    ZsCosa (29th July 2013)

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

    Default Re: Draw a line animation

    Thanks alot!! I'll try that and get back later.

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

    Default Re: Draw a line animation

    Hi, so here is what I got for myLine.h
    Qt Code:
    1. #ifndef MYLINE_H
    2. #define MYLINE_H
    3.  
    4. #include <QGraphicsLineItem>
    5.  
    6. class myLine : public QObject, public QGraphicsLineItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. myLine();
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13.  
    14. public slots:
    15. void mySlot();
    16. private:
    17. QLineF thisline;
    18.  
    19. };
    20. #endif // MYLINE_H
    To copy to clipboard, switch view to plain text mode 
    myLine.cpp just to draw the line and try to implement the SLOT()
    Qt Code:
    1. #include "myLine.h"
    2.  
    3. #include <QPainter>
    4. #include <QApplication>
    5.  
    6. myLine::myLine()
    7. {
    8. thisline.setLine(0,0,50,50);
    9. }
    10.  
    11. void myLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    12. {
    13. painter->setRenderHint(QPainter::Antialiasing);
    14. painter->setPen(QPen(Qt::red, 8, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
    15. painter->setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
    16. painter->drawLine(thisline);
    17. }
    18.  
    19.  
    20. void myLine::mySlot()
    21. {
    22. for (int i = 1; i < 100; i++)
    23. {
    24. QLineF line = this->line();
    25. line.setLine(0,0,50+i,50+i);
    26. update();
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    There's a Dialog to display
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsView>
    7. #include <QTimer>
    8.  
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20.  
    21. private:
    22. Ui::Dialog *ui;
    23. QTimer *timer;
    24. };
    25.  
    26. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    And the source for dialog.cpp where things happen!
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "myLine.h"
    4.  
    5. #include <QGraphicsView>
    6.  
    7. Dialog::Dialog(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::Dialog)
    10. {
    11. ui->setupUi(this);
    12.  
    13. scene = new QGraphicsScene(this);
    14.  
    15. ui->graphicsView->setScene(scene);
    16. ui->graphicsView->setSceneRect(0,0,700,700);
    17. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    18.  
    19. myLine *line = new myLine();
    20. scene->addItem(line);
    21.  
    22. timer = new QTimer(this);
    23. connect(timer, SIGNAL(timeout()), line, SLOT(mySlot()));
    24. timer->start(100);
    25.  
    26. }
    27.  
    28. Dialog::~Dialog()
    29. {
    30. delete ui;
    31. }
    To copy to clipboard, switch view to plain text mode 

    - when compiles it only prints out the first line which is (0,0,50,50) I think and there's no animation. I've been trying to figure it out, can you please tell me what is wrong here? And thanks for being patience with me, I'm learning so... pardon my noobish!

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  14. #12
    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 ?

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  16. #14
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.