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!
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!
ZsCosa (27th July 2013)
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.
It could have but I don't see the point of looking for more solutions if you already have two to choose from.
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!
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.
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
And create a timer:Qt Code:
{ painter->drawLine(point1,point2); }To copy to clipboard, switch view to plain text mode
Qt Code:
connect(timer, SIGNAL(timeout()), scene, SLOT(advance())); 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.
Thank you.Qt Code:
void myitems::advance(int phase) { if(!phase) return; }To copy to clipboard, switch view to plain text mode
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.
ZsCosa (29th July 2013)
Thanks alot!! I'll try that and get back later.
Hi, so here is what I got for myLine.h
myLine.cpp just to draw the line and try to implement the SLOT()Qt Code:
#ifndef MYLINE_H #define MYLINE_H #include <QGraphicsLineItem> { Q_OBJECT public: myLine(); public slots: void mySlot(); private: QLineF thisline; }; #endif // MYLINE_HTo copy to clipboard, switch view to plain text mode
Qt Code:
#include "myLine.h" #include <QPainter> #include <QApplication> myLine::myLine() { thisline.setLine(0,0,50,50); } { painter->drawLine(thisline); } void myLine::mySlot() { for (int i = 1; i < 100; i++) { line.setLine(0,0,50+i,50+i); update(); } }To copy to clipboard, switch view to plain text mode
There's a Dialog to display
And the source for dialog.cpp where things happen!Qt Code:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QGraphicsScene> #include <QGraphicsView> #include <QTimer> namespace Ui { class Dialog; } { Q_OBJECT public: ~Dialog(); private: Ui::Dialog *ui; QGraphicsScene *scene; QTimer *timer; }; #endif // DIALOG_HTo copy to clipboard, switch view to plain text mode
Qt Code:
#include "dialog.h" #include "ui_dialog.h" #include "myLine.h" #include <QGraphicsView> ui(new Ui::Dialog) { ui->setupUi(this); ui->graphicsView->setScene(scene); ui->graphicsView->setSceneRect(0,0,700,700); myLine *line = new myLine(); scene->addItem(line); connect(timer, SIGNAL(timeout()), line, SLOT(mySlot())); timer->start(100); } Dialog::~Dialog() { delete ui; }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!
The code looks fine (more or less). The problem has to be elsewhere (e.g. the event loop not working).
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 ?
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.
Remember to run the event loop.Qt Code:
Q_OBJECT public: // ... public slots: l.setP2(pt); line->setLine(l); } }; // ... QPropertyAnimation *anim = new QPropertyAnimation(&dialog, "endPoint"); anim->setStartValue(...); anim->setEndValue(...); anim->setDuration(...); anim->start();To copy to clipboard, switch view to plain text mode
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
dialog.cppQt Code:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QGraphicsScene> #include <QGraphicsView> #include <QTimer> #include <QGraphicsLineItem> namespace Ui { class Dialog; } { Q_OBJECT public: ~Dialog(); public slots: private: Ui::Dialog *ui; QGraphicsScene *scene; QTimer *timer; QGraphicsLineItem *myLine; }; #endif // DIALOG_HTo copy to clipboard, switch view to plain text mode
Qt Code:
#include "dialog.h" #include "ui_dialog.h" #include <QGraphicsView> #include <QPropertyAnimation> ui(new Ui::Dialog) { ui->setupUi(this); ui->graphicsView->setScene(scene); ui->graphicsView->setSceneRect(0,0,300,300); scene->addItem(myLine); QPropertyAnimation *anim = new QPropertyAnimation(this, "endPoint"); anim->setDuration(1000); anim->start(); } Dialog::~Dialog() { delete ui; } { return l.p2(); } { l.setP2(pt); myLine->setLine(l); }To copy to clipboard, switch view to plain text mode
Last edited by ZsCosa; 30th July 2013 at 15:16.
Bookmarks