PDA

View Full Version : Problem in feteching time and date.



Niamita
14th June 2011, 10:40
Hello everyone
I have facing problem in feteching time and date.The date and time i fetched but the problem is it is not updating with the system time and date. When i run the application it is giving the time and date but if the application is open then the time is not getting update , it update when i again run the application.

One more thing the format of time is in hr.min.sec while i want to fetch it like hr.min.
Thank you for help

MarekR22
14th June 2011, 10:46
see example (http://doc.qt.nokia.com/latest/widgets-analogclock.html).

and QTime::toString

Niamita
14th June 2011, 11:04
i have seen the example and my code is

QTime time = QTime::currentTime();
QString timeString = time.toString();
label->setText(timeString);
startTimer(1000);

but the time is not getting update. and the format i also have to change. How can i do all that.

almboa
14th June 2011, 11:51
you have to specify a time format in the toString()-method... see documentation.

Where is that snippet of your code? Is it in a slot which is called every second? Did you connect the timeout()-Signal from the QTimer to this slot?
What is the startTimer()-function? Did you write it on your own to call timer.start() or do you use the QObject::startTimer()-function? In the second case you need to reimplement the timerEvent(QTimerEvent *)-function.

Niamita
14th June 2011, 12:32
I implement timerEvent(QTimerEvent *)-function and in that function i update the time like

label->setText(timeString);
but the time is not getting update.

wysota
14th June 2011, 13:30
Do you re-ask for the time in the timer event?

Niamita
15th June 2011, 04:55
No i am not reasking time in timer event. i just update the label text in timer event.

Santosh Reddy
15th June 2011, 05:10
Check your code against this


class MyClass : QObject {
...
protected:
void timerEvent(QTimerEvent * event);
}

void MyClass::MyClass(QObject * parent)
{
...
mytime = startTimer(1000);
}

void MyClass::timerEvent(QTimerEvent * event)
{
QTime time = QTime::currentTime();
QString timeString = time.toString("hh:mm");
label->setText(timeString);
}

Niamita
16th June 2011, 04:34
Hi
Thaks for the answer but i am facing with a problem that i set timer at two places one to update paint and one to update time , but both are not working right, they collapsed.
How to handle both of them simultanously.

Santosh Reddy
16th June 2011, 05:05
i set timer at two places one to update paint and one to update time
Ok you can use two timers like this


class MyClass : QObject {
...
protected:
void timerEvent(QTimerEvent * event);

private:
int timeUpdateTimer;
int paintUpdateTimer;
}

void MyClass::MyClass(QObject * parent)
{
timeUpdateTimer = 0; // Just to be safe.
paintUpdateTimer = 0;
...
timeUpdateTimer = startTimer(1000); // Update time every 1 second
paintUpdateTimer= startTimer(500); // Update paint every 0.5 second, you can move this to any place in you class
}

void MyClass::timerEvent(QTimerEvent * event)
{
if(timeUpdateTimer == event->timerID())
{
QTime time = QTime::currentTime();
QString timeString = time.toString("hh:mm");
label->setText(timeString);
}
else if(paintUpdateTimer == event->timerID())
{
widget->update(); //update paint here
}
else
{
killTimer(event->timerId()); //Have this, till you clearly understand how QObject, abd QTimers work
}
}