PDA

View Full Version : Qt timer problem



anh5kor
14th October 2014, 15:29
Hello,

I want to add a time on my design which starts with formate 00:00:00 and my number should keep on increasing every seconds.
I tried to add the code


showtime()
{
QTime time =QTime::currentTime();
QString time_text = time.toString("hh:mm:ss");
ui.clock->setText(time_text);
}
constructor()
{
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(showtime ()));
timer->start();
}


but I am getting my system time.
I want it should start from zero and keep on increasing every second.
Please let me know some solution.

west
14th October 2014, 16:24
I guess this solution is sufficient for you ;)



constructor : m_time(QTime(0,0,0))
{
ui.clock->setText(m_time.toString("hh:mm:ss"));
}

showtime
{
m_time = m_time.addSecs(1);
ui.clock->setText(m_time.toString("hh:mm:ss"));
}

Added after 6 minutes:

To be more precise:



constructor : m_time(QTime::currentTime())
{
ui.clock->setText(QTime(0,0,0).toString("hh:mm:ss"));
}

showtime
{
ui.clock->setText(QTime(0,0,0).addSecs(m_time.secsTo(QTime:: currentTime())).toString("hh:mm:ss"));
}

Lesiok
14th October 2014, 17:21
Use QElapsedTimer instead of QTimer.

anda_skoa
14th October 2014, 18:45
Use QElapsedTimer instead of QTimer.
You meant QElapsedTimer instead of QTime?

QTimer is an "active" class, it has a signal.

Cheers,
_

Lesiok
15th October 2014, 07:44
You meant QElapsedTimer instead of QTime?

QTimer is an "active" class, it has a signal.

Cheers,
_

Of course QTime, thanks anda_skoa for correction.