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
){
ui.setupUi(this);
time = 0;
timeInterval = 1; // this sets the time interval at 1 millisecond
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
ui.timeLine->setText(str);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Is this the right way to use QTimer or am I missing something?
Bookmarks