PDA

View Full Version : QTimer advice



qt_beginner
10th May 2010, 12:47
Hi,

I'm a relative newbie to Qt but am so far enjoying it. I have a question about QTimer. I have it working in an application but am just wondering if I am going about it the correct way.


#include "timerExample.h"
#include <QtGui>

timerExample::timerExample(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
time = 0;
timeInterval = 1; // this sets the time interval at 1 millisecond
timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
}


void timerExample::update()
{
time = time + timeInterval;
}


void timerExample::on_threadStartButton_clicked()
{
timer->start(timeInterval);
}

void timerExample::on_threadStopButton_clicked()
{
timer->stop();

// writes out time taken to a LineEdit
QString str = QString::number(time);
ui.timeLine->setText(str);
}



Is this the right way to use QTimer or am I missing something?

Lykurg
10th May 2010, 14:02
Theoretical it's ok, but in your case nonsens, and 1 millisecond is normally too short (a function call every millisecond...). In your case better have a look at QTime and QTime::elapsed().

qt_beginner
10th May 2010, 14:16
Thanks for your input. I see that that function is exactly what I need but I'm using Qt 4.4. I can't see a similar method to that described in this version.

Lykurg
10th May 2010, 15:33
Well, it must be there: http://doc.trolltech.com/4.3/qtime.html#elapsed. But anyway, then do it yourself. On first button take the value of QTime::currentTime() and on the second button also and then calculate the difference between both.