#include <QtGui>
{
Q_OBJECT
int nbFrames;
qreal angle, step, radius ;
int interval;
public :
View(void)
nbFrames(100),
angle(0), // start angle=0°
step(360.0/nbFrames), // 1 round == 852 frames
radius(150.0), // circle radius
interval(1)
{
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 ;
connect(timer,SIGNAL(timeout()),this,SLOT(doTimer()));
timer->start(1);
}
protected slots:
void doTimer()
{
static int timerCounter = 0;
++timerCounter;
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 ;
//the timer doesn't take into account the time you spend here, why do you?
t0 = t1;
// no restarting..
//timer->start(interval);
}
else
{
if (timerCounter % interval == 0)
{
// 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 ;
}
}
}
} ;
#include <QtGui>
class View : public QGraphicsView
{
Q_OBJECT
int nbFrames;
QGraphicsScene *scene ;
QGraphicsRectItem *item ;
qreal angle, step, radius ;
QTime t0, t1 ;
int interval;
QTimer* timer;
public :
View(void)
: scene(new QGraphicsScene),
nbFrames(100),
angle(0), // start angle=0°
step(360.0/nbFrames), // 1 round == 852 frames
radius(150.0), // circle radius
interval(1)
{
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 ;
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(doTimer()));
timer->start(1);
}
protected slots:
void doTimer()
{
static int timerCounter = 0;
++timerCounter;
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 ;
//the timer doesn't take into account the time you spend here, why do you?
t0 = t1;
// no restarting..
//timer->start(interval);
}
else
{
if (timerCounter % interval == 0)
{
// 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 ;
}
}
}
} ;
To copy to clipboard, switch view to plain text mode
start at "11:55:14.630" , interval = 1
round time : 0.132 s, should be 0.1 s (diff = 0.032 ( 24.2424 %) for 1 )
round time : 0.2 s, should be 0.2 s (diff = -6.93889e-18 ( -3.46945e-15 %) for 2 )
round time : 0.3 s, should be 0.3 s (diff = 1.73472e-17 ( 5.78241e-15 %) for 3 )
round time : 0.4 s, should be 0.4 s (diff = -1.38778e-17 ( -3.46945e-15 %) for 4 )
round time : 0.5 s, should be 0.5 s (diff = 1.04083e-17 ( 2.08167e-15 %) for 5 )
round time : 0.603 s, should be 0.6 s (diff = 0.003 ( 0.497512 %) for 6 )
round time : 0.7 s, should be 0.7 s (diff = -5.20417e-17 ( -7.43453e-15 %) for 7 )
round time : 0.8 s, should be 0.8 s (diff = -2.77556e-17 ( -3.46945e-15 %) for 8 )
round time : 0.9 s, should be 0.9 s (diff = -3.46945e-18 ( -3.85494e-16 %) for 9 )
round time : 1 s, should be 1 s (diff = 2.08167e-17 ( 2.08167e-15 %) for 10 )
round time : 1.1 s, should be 1.1 s (diff = -6.59195e-17 ( -5.99268e-15 %) for 11 )
round time : 1.2 s, should be 1.2 s (diff = 6.93889e-17 ( 5.78241e-15 %) for 12 )
round time : 1.302 s, should be 1.3 s (diff = 0.002 ( 0.15361 %) for 13 )
round time : 1.4 s, should be 1.4 s (diff = -1.04083e-16 ( -7.43453e-15 %) for 14 )
round time : 1.5 s, should be 1.5 s (diff = 3.1225e-17 ( 2.08167e-15 %) for 15 )
round time : 1.6 s, should be 1.6 s (diff = -5.55112e-17 ( -3.46945e-15 %) for 16 )
round time : 1.7 s, should be 1.7 s (diff = 7.97973e-17 ( 4.69396e-15 %) for 17 )
round time : 1.8 s, should be 1.8 s (diff = -6.93889e-18 ( -3.85494e-16 %) for 18 )
round time : 1.9 s, should be 1.9 s (diff = -9.36751e-17 ( -4.93027e-15 %) for 19 )
round time : 2 s, should be 2 s (diff = 4.16334e-17 ( 2.08167e-15 %) for 20 )
round time : 2.1 s, should be 2.1 s (diff = -4.51028e-17 ( -2.14775e-15 %) for 21 )
round time : 2.2 s, should be 2.2 s (diff = -1.31839e-16 ( -5.99268e-15 %) for 22 )
round time : 2.301 s, should be 2.3 s (diff = 0.001 ( 0.0434594 %) for 23 )
round time : 2.4 s, should be 2.4 s (diff = 1.38778e-16 ( 5.78241e-15 %) for 24 )
round time : 2.5 s, should be 2.5 s (diff = 5.20417e-17 ( 2.08167e-15 %) for 25 )
round time : 2.6 s, should be 2.6 s (diff = -3.46945e-17 ( -1.3344e-15 %) for 26 )
round time : 2.7 s, should be 2.7 s (diff = -1.21431e-16 ( -4.49743e-15 %) for 27 )
round time : 2.8 s, should be 2.8 s (diff = -2.08167e-16 ( -7.43453e-15 %) for 28 )
round time : 2.901 s, should be 2.9 s (diff = 0.001 ( 0.0344709 %) for 29 )
round time : 3 s, should be 3 s (diff = 6.245e-17 ( 2.08167e-15 %) for 30 )
round time : 3.1 s, should be 3.1 s (diff = -2.42861e-17 ( -7.83424e-16 %) for 31 )
round time : 3.2 s, should be 3.2 s (diff = -1.11022e-16 ( -3.46945e-15 %) for 32 )
round time : 3.3 s, should be 3.3 s (diff = -1.97758e-16 ( -5.99268e-15 %) for 33 )
round time : 3.4 s, should be 3.4 s (diff = 1.59595e-16 ( 4.69396e-15 %) for 34 )
round time : 3.501 s, should be 3.5 s (diff = 0.001 ( 0.0285633 %) for 35 )
round time : 3.6 s, should be 3.6 s (diff = -1.38778e-17 ( -3.85494e-16 %) for 36 )
round time : 3.7 s, should be 3.7 s (diff = -1.00614e-16 ( -2.7193e-15 %) for 37 )
round time : 3.8 s, should be 3.8 s (diff = -1.8735e-16 ( -4.93027e-15 %) for 38 )
round time : 3.9 s, should be 3.9 s (diff = 1.70003e-16 ( 4.35905e-15 %) for 39 )
round time : 4.001 s, should be 4 s (diff = 0.001 ( 0.0249938 %) for 40 )
round time : 4.1 s, should be 4.1 s (diff = 4.4062e-16 ( 1.07468e-14 %) for 41 )
round time : 4.2 s, should be 4.2 s (diff = -9.02056e-17 ( -2.14775e-15 %) for 42 )
round time : 4.3 s, should be 4.3 s (diff = 2.67147e-16 ( 6.21273e-15 %) for 43 )
round time : 4.401 s, should be 4.4 s (diff = 0.001 ( 0.0227221 %) for 44 )
round time : 4.5 s, should be 4.5 s (diff = 9.36751e-17 ( 2.08167e-15 %) for 45 )
round time : 4.6 s, should be 4.6 s (diff = -4.3715e-16 ( -9.50327e-15 %) for 46 )
round time : 4.7 s, should be 4.7 s (diff = -7.97973e-17 ( -1.69781e-15 %) for 47 )
round time : 4.801 s, should be 4.8 s (diff = 0.001 ( 0.020829 %) for 48 )
round time : 4.9 s, should be 4.9 s (diff = -2.5327e-16 ( -5.16877e-15 %) for 49 )
round time : 5 s, should be 5 s (diff = 1.04083e-16 ( 2.08167e-15 %) for 50 )
round time : 5.101 s, should be 5.1 s (diff = 0.001 ( 0.019604 %) for 51 )
round time : 5.2 s, should be 5.2 s (diff = -6.93889e-17 ( -1.3344e-15 %) for 52 )
round time : 5.3 s, should be 5.3 s (diff = 2.87964e-16 ( 5.43328e-15 %) for 53 )
round time : 5.401 s, should be 5.4 s (diff = 0.001 ( 0.0185151 %) for 54 )
round time : 5.5 s, should be 5.5 s (diff = 1.14492e-16 ( 2.08167e-15 %) for 55 )
round time : 5.6 s, should be 5.6 s (diff = -4.16334e-16 ( -7.43453e-15 %) for 56 )
round time : 5.7 s, should be 5.7 s (diff = -5.89806e-17 ( -1.03475e-15 %) for 57 )
start at "11:55:14.630" , interval = 1
round time : 0.132 s, should be 0.1 s (diff = 0.032 ( 24.2424 %) for 1 )
round time : 0.2 s, should be 0.2 s (diff = -6.93889e-18 ( -3.46945e-15 %) for 2 )
round time : 0.3 s, should be 0.3 s (diff = 1.73472e-17 ( 5.78241e-15 %) for 3 )
round time : 0.4 s, should be 0.4 s (diff = -1.38778e-17 ( -3.46945e-15 %) for 4 )
round time : 0.5 s, should be 0.5 s (diff = 1.04083e-17 ( 2.08167e-15 %) for 5 )
round time : 0.603 s, should be 0.6 s (diff = 0.003 ( 0.497512 %) for 6 )
round time : 0.7 s, should be 0.7 s (diff = -5.20417e-17 ( -7.43453e-15 %) for 7 )
round time : 0.8 s, should be 0.8 s (diff = -2.77556e-17 ( -3.46945e-15 %) for 8 )
round time : 0.9 s, should be 0.9 s (diff = -3.46945e-18 ( -3.85494e-16 %) for 9 )
round time : 1 s, should be 1 s (diff = 2.08167e-17 ( 2.08167e-15 %) for 10 )
round time : 1.1 s, should be 1.1 s (diff = -6.59195e-17 ( -5.99268e-15 %) for 11 )
round time : 1.2 s, should be 1.2 s (diff = 6.93889e-17 ( 5.78241e-15 %) for 12 )
round time : 1.302 s, should be 1.3 s (diff = 0.002 ( 0.15361 %) for 13 )
round time : 1.4 s, should be 1.4 s (diff = -1.04083e-16 ( -7.43453e-15 %) for 14 )
round time : 1.5 s, should be 1.5 s (diff = 3.1225e-17 ( 2.08167e-15 %) for 15 )
round time : 1.6 s, should be 1.6 s (diff = -5.55112e-17 ( -3.46945e-15 %) for 16 )
round time : 1.7 s, should be 1.7 s (diff = 7.97973e-17 ( 4.69396e-15 %) for 17 )
round time : 1.8 s, should be 1.8 s (diff = -6.93889e-18 ( -3.85494e-16 %) for 18 )
round time : 1.9 s, should be 1.9 s (diff = -9.36751e-17 ( -4.93027e-15 %) for 19 )
round time : 2 s, should be 2 s (diff = 4.16334e-17 ( 2.08167e-15 %) for 20 )
round time : 2.1 s, should be 2.1 s (diff = -4.51028e-17 ( -2.14775e-15 %) for 21 )
round time : 2.2 s, should be 2.2 s (diff = -1.31839e-16 ( -5.99268e-15 %) for 22 )
round time : 2.301 s, should be 2.3 s (diff = 0.001 ( 0.0434594 %) for 23 )
round time : 2.4 s, should be 2.4 s (diff = 1.38778e-16 ( 5.78241e-15 %) for 24 )
round time : 2.5 s, should be 2.5 s (diff = 5.20417e-17 ( 2.08167e-15 %) for 25 )
round time : 2.6 s, should be 2.6 s (diff = -3.46945e-17 ( -1.3344e-15 %) for 26 )
round time : 2.7 s, should be 2.7 s (diff = -1.21431e-16 ( -4.49743e-15 %) for 27 )
round time : 2.8 s, should be 2.8 s (diff = -2.08167e-16 ( -7.43453e-15 %) for 28 )
round time : 2.901 s, should be 2.9 s (diff = 0.001 ( 0.0344709 %) for 29 )
round time : 3 s, should be 3 s (diff = 6.245e-17 ( 2.08167e-15 %) for 30 )
round time : 3.1 s, should be 3.1 s (diff = -2.42861e-17 ( -7.83424e-16 %) for 31 )
round time : 3.2 s, should be 3.2 s (diff = -1.11022e-16 ( -3.46945e-15 %) for 32 )
round time : 3.3 s, should be 3.3 s (diff = -1.97758e-16 ( -5.99268e-15 %) for 33 )
round time : 3.4 s, should be 3.4 s (diff = 1.59595e-16 ( 4.69396e-15 %) for 34 )
round time : 3.501 s, should be 3.5 s (diff = 0.001 ( 0.0285633 %) for 35 )
round time : 3.6 s, should be 3.6 s (diff = -1.38778e-17 ( -3.85494e-16 %) for 36 )
round time : 3.7 s, should be 3.7 s (diff = -1.00614e-16 ( -2.7193e-15 %) for 37 )
round time : 3.8 s, should be 3.8 s (diff = -1.8735e-16 ( -4.93027e-15 %) for 38 )
round time : 3.9 s, should be 3.9 s (diff = 1.70003e-16 ( 4.35905e-15 %) for 39 )
round time : 4.001 s, should be 4 s (diff = 0.001 ( 0.0249938 %) for 40 )
round time : 4.1 s, should be 4.1 s (diff = 4.4062e-16 ( 1.07468e-14 %) for 41 )
round time : 4.2 s, should be 4.2 s (diff = -9.02056e-17 ( -2.14775e-15 %) for 42 )
round time : 4.3 s, should be 4.3 s (diff = 2.67147e-16 ( 6.21273e-15 %) for 43 )
round time : 4.401 s, should be 4.4 s (diff = 0.001 ( 0.0227221 %) for 44 )
round time : 4.5 s, should be 4.5 s (diff = 9.36751e-17 ( 2.08167e-15 %) for 45 )
round time : 4.6 s, should be 4.6 s (diff = -4.3715e-16 ( -9.50327e-15 %) for 46 )
round time : 4.7 s, should be 4.7 s (diff = -7.97973e-17 ( -1.69781e-15 %) for 47 )
round time : 4.801 s, should be 4.8 s (diff = 0.001 ( 0.020829 %) for 48 )
round time : 4.9 s, should be 4.9 s (diff = -2.5327e-16 ( -5.16877e-15 %) for 49 )
round time : 5 s, should be 5 s (diff = 1.04083e-16 ( 2.08167e-15 %) for 50 )
round time : 5.101 s, should be 5.1 s (diff = 0.001 ( 0.019604 %) for 51 )
round time : 5.2 s, should be 5.2 s (diff = -6.93889e-17 ( -1.3344e-15 %) for 52 )
round time : 5.3 s, should be 5.3 s (diff = 2.87964e-16 ( 5.43328e-15 %) for 53 )
round time : 5.401 s, should be 5.4 s (diff = 0.001 ( 0.0185151 %) for 54 )
round time : 5.5 s, should be 5.5 s (diff = 1.14492e-16 ( 2.08167e-15 %) for 55 )
round time : 5.6 s, should be 5.6 s (diff = -4.16334e-16 ( -7.43453e-15 %) for 56 )
round time : 5.7 s, should be 5.7 s (diff = -5.89806e-17 ( -1.03475e-15 %) for 57 )
To copy to clipboard, switch view to plain text mode
#ifdef Q_OS_WIN
#include "windows.h"
DWORD AdjustPrivileges()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// get the current process token handle
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return GetLastError();
}
// get the LUID for system-time privilege.
LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// get set-time privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
// cannot test return value of AdjustTokenPrivileges.
return GetLastError();
}
#endif
..
AdjustPrivileges();
if (timeBeginPeriod(1) == TIMERR_NOCANDO)
qWarning() << "Could not start the time period!";
...
timeEndPeriod(1);
#ifdef Q_OS_WIN
#include "windows.h"
DWORD AdjustPrivileges()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// get the current process token handle
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return GetLastError();
}
// get the LUID for system-time privilege.
LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// get set-time privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
// cannot test return value of AdjustTokenPrivileges.
return GetLastError();
}
#endif
..
AdjustPrivileges();
if (timeBeginPeriod(1) == TIMERR_NOCANDO)
qWarning() << "Could not start the time period!";
...
timeEndPeriod(1);
To copy to clipboard, switch view to plain text mode
Bookmarks