Qwidget Animation with Qtimer
I want to make an animation with two circlewidget in a scene. My purpose is to make the first widget with concentric circle animated for a periode of time and then stop it, after make the second widget animated also for a periode of time .
but I get blocked how to make such thing here is my code:
Main.cpp:
Code:
#include <QApplication>
#include <QTimer>
#include<unistd.h>
#include<QGraphicsScene>
#include<QGraphicsProxyWidget>
#include<QGraphicsView>
#include "circlewidget.h"
#include<QTimeLine>
int main(int argc, char *argv[])
{
CircleWidget * circle= new CircleWidget();
CircleWidget * circl= new CircleWidget();
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(circle);
proxy->setPos(0,1);
scene.addItem(proxy);
QGraphicsProxyWidget *prox = new QGraphicsProxyWidget();
prox->setWidget(circl);
prox->setPos(200,100);
scene.addItem(prox);
/*QTimeLine *timer = new QTimeLine(5000);
timer->setFrameRange(0, 100);*/
QObject::connect(timer,
SIGNAL(timeout
()), circl,
SLOT(nextAnimationFrame
()));
timer->start();
sleep(5);
timer->stop();
QObject::connect(time,
SIGNAL(timeout
()),circle,
SLOT(nextAnimationFrame
()));
time->start();
view.show();
return app.exec();
}
circlewidget.cpp:
Code:
#include <QtGui>
#include "circlewidget.h"
#include<unistd.h>
#include <stdlib.h>
CircleWidget
::CircleWidget(QWidget *parent
) {
}
QSize CircleWidget
::minimumSizeHint() const {
}
QSize CircleWidget
::sizeHint() const {
}
void CircleWidget::nextAnimationFrame()
{
update();
++ frameNo;
}
{
painter.
setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
for (int diameter = 0; diameter < 170; diameter += 6) {
int delta = abs((frameNo %120) - diameter / 2);
int alpha = 250 - (delta * delta) / 3 - diameter;
if (alpha > 0) {
painter.
setPen(QPen(QColor(166, diameter
/ 2,
32, alpha
),
5));
painter.
drawEllipse(QRectF(-diameter
/ 2.0,
-diameter
/ 2.0,
diameter, diameter));
}
}
}
ciclewidget.h:
Code:
#ifndef CIRCLEWIDGET_H
#define CIRCLEWIDGET_H
#include <QWidget>
class CircleWidget
: public QWidget {
Q_OBJECT
public:
QSize minimumSizeHint
() const;
public slots:
void nextAnimationFrame();
protected:
private:
int frameNo;
};
#endif
Re: Qwidget Animation with Qtimer
You start the timer but the pause the main thread for 5 seconds.
You then stop the timer.
So the timer never ever has a chance to actually run.
QtWidgets is an event based UI framework, so you need to run an event loop to let it process events, such as paint requests, user input or timers.
the line
does that.
What you could do is to use QTimer::singleShot() to delay the start of the second timer.
Cheers,
_
Re: Qwidget Animation with Qtimer
thx for you're response,
I used the QTimer::singleShot() but the animation didn't want to start !
Qt Code:
QObject::connect(timer, SIGNAL(timeout()), circl, SLOT(nextAnimationFrame()));
timer->start();
QTimer::singleShot(20,circle,SLOT(nextAnimationFra me()));
view.show();
Re: Qwidget Animation with Qtimer
Please show your current main().
Re: Qwidget Animation with Qtimer
Code:
#include <QApplication>
#include <QTimer>
#include<unistd.h>
#include<QGraphicsScene>
#include<QGraphicsProxyWidget>
#include<QGraphicsView>
#include "circlewidget.h"
#include<QTimeLine>
int main(int argc, char *argv[])
{
CircleWidget * circle= new CircleWidget();
CircleWidget * circl= new CircleWidget();
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(circle);
proxy->setPos(0,1);
scene.addItem(proxy);
QGraphicsProxyWidget *prox = new QGraphicsProxyWidget();
prox->setWidget(circl);
prox->setPos(200,100);
scene.addItem(prox);
/*QTimeLine *timer = new QTimeLine(5000);
timer->setFrameRange(0, 100);*/
QObject::connect(timer,
SIGNAL(timeout
()), circl,
SLOT(nextAnimationFrame
()));
timer->start();
QTimer::singleShot(200,circle,
SLOT(nextAnimationFrame
()));
view.show();
return app.exec();
}
Added after 59 minutes:
My goal is to make animation with animated widget ( which are the concentric circle widget ) .....May I use the thread in this case ?!
Re: Qwidget Animation with Qtimer
My suggestion was to use the single shot timer to start the second animation timer
Code:
QTimer::singleShot(5000,
time,
SLOT(start
()));
You should probably also set some timer intervals.
Cheers,
_
Re: Qwidget Animation with Qtimer
What exactly is supposed to be animated? Your nextAnimationFrame() only increments some integer.
Re: Qwidget Animation with Qtimer
But it's animated u can test it if you want :3
thx anda_skoa :) you helped me ;)
Re: Qwidget Animation with Qtimer
You shouldn't animate it like that. Regardless if it works or not.
Re: Qwidget Animation with Qtimer
But how should I animate it?! :( ....they are circles....I want to do resizeable and smoothly animated circle. So I worked on the diametre to create a concentric circle .
It's not like that I should work ? you make me confused :(
Re: Qwidget Animation with Qtimer
Quote:
Originally Posted by
tissa
But how should I animate it?! :( ....they are circles....I want to do resizeable and smoothly animated circle
So create property for the size of the circle and animate that property instead of some "frameNo" integer using some "nextAnimationFrame" function. If you set the diameter to "10" then it is supposed to be "10", if you set it to "16" then it should be "16". Right now you can only increase it by 1 in a rather unreliable manner.