#include <QApplication>
#include <QtGui>
qreal interval = 1.0 ;
const qreal nbFrames = 100.0 ;
{
Q_OBJECT
int timerid ;
qreal angle, step, radius ;
public :
View(void)
angle(0), // start angle=0°
step(360.0/nbFrames), // 1 round == 852 frames
radius(150.0) // circle radius
{
scene->setBackgroundBrush(Qt::darkBlue) ;
setScene(scene) ;
// draw circle path
// draw shape
item->setBrush(Qt::darkYellow) ;
item
->setPen
(QPen(Qt
::black)) ;
// draw start point
start->setBrush(Qt::red) ;
start
->setPen
(QPen(Qt
::black)) ;
// start animation
t0
= QTime::currentTime() ;
qDebug() << "start at" << t0.toString("hh:mm:ss.zzz") << ", interval =" << interval ;
timerid = startTimer(interval) ;
}
protected :
{
if(angle>=360)
{
// compute time and restart
t1
= QTime::currentTime() ;
qreal theoric = 0.001*interval*nbFrames ;
qreal measured = 0.001*t0.msecsTo(t1) ;
qreal diff = (measured-theoric) ;
qreal delta = 100*diff/measured ;
qDebug() << "round time :" << measured << "s, should be" << theoric << "s (diff ="<<diff<<"("<<delta<<"%) for"<<interval<<")" ;
killTimer(timerid) ;
interval++ ; // decrease framerate
angle = 0 ;
t0
= QTime::currentTime() ;
timerid = startTimer(interval) ;
}
else
{
// compute new position
qreal radians = M_PI*angle/180.0 ;
qreal x = radius*cos(radians) ;
qreal y = radius*sin(radians) ;
item->setPos(x,y) ;
// step ahead
angle += step ;
}
}
} ;
int main(int argc, char** argv)
{
View view ;
view.
setGeometry(QRect(100,
100,
600,
500)) ;
view.show() ;
return app.exec() ;
}
#include "main.moc"
#include <QApplication>
#include <QtGui>
qreal interval = 1.0 ;
const qreal nbFrames = 100.0 ;
class View : public QGraphicsView
{
Q_OBJECT
QGraphicsScene *scene ;
QGraphicsRectItem *item ;
int timerid ;
qreal angle, step, radius ;
QTime t0, t1 ;
public :
View(void)
: scene(new QGraphicsScene),
angle(0), // start angle=0°
step(360.0/nbFrames), // 1 round == 852 frames
radius(150.0) // circle radius
{
scene->setBackgroundBrush(Qt::darkBlue) ;
setScene(scene) ;
// draw circle path
new QGraphicsEllipseItem(QRectF(-radius,-radius,2*radius,2*radius),0,scene) ;
// draw shape
item = new QGraphicsRectItem(QRectF(-25,-15,50,30),0,scene) ;
item->setBrush(Qt::darkYellow) ;
item->setPen(QPen(Qt::black)) ;
// draw start point
QGraphicsEllipseItem *start = new QGraphicsEllipseItem(QRectF(radius-2,-2,4,4),0,scene) ;
start->setBrush(Qt::red) ;
start->setPen(QPen(Qt::black)) ;
// start animation
t0 = QTime::currentTime() ;
qDebug() << "start at" << t0.toString("hh:mm:ss.zzz") << ", interval =" << interval ;
timerid = startTimer(interval) ;
}
protected :
void timerEvent(QTimerEvent *e)
{
if(angle>=360)
{
// compute time and restart
t1 = QTime::currentTime() ;
qreal theoric = 0.001*interval*nbFrames ;
qreal measured = 0.001*t0.msecsTo(t1) ;
qreal diff = (measured-theoric) ;
qreal delta = 100*diff/measured ;
qDebug() << "round time :" << measured << "s, should be" << theoric << "s (diff ="<<diff<<"("<<delta<<"%) for"<<interval<<")" ;
killTimer(timerid) ;
interval++ ; // decrease framerate
angle = 0 ;
t0 = QTime::currentTime() ;
timerid = startTimer(interval) ;
}
else
{
// compute new position
qreal radians = M_PI*angle/180.0 ;
qreal x = radius*cos(radians) ;
qreal y = radius*sin(radians) ;
item->setPos(x,y) ;
// step ahead
angle += step ;
}
}
} ;
int main(int argc, char** argv)
{
QApplication app(argc,argv) ;
View view ;
view.setGeometry(QRect(100,100,600,500)) ;
view.show() ;
return app.exec() ;
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks