View Full Version : How to make the time run in a GUI using QThreads ?

11th July 2013, 12:27
Hello all ,
I am Making a Gui to show a running time in a text label,I tried doing it buut i am only to make a time stamp ,I cannot make the time run at all.
It seems to me that the only method to do it is by using thread . I am unfamiliar with thread at all .Let alone QThread.Is there any other way to make the time run ?
Or can someone help me out with threads ? Please help thank you in advance .

Santosh Reddy
11th July 2013, 13:05
One quick way

class TimeLabel : public QLabel
explicit TimeLabel(QWidget * parent = 0)
: QLabel(parent)
startTimer(50); // Update time every 50 ms
void timerEvent(QTimerEvent *)

int main(int argc, char ** argv)
QApplication app(argc, argv);

TimeLabel label;

return app.exec();
#include "main.moc"

12th July 2013, 02:57
Hey thanks for the quick reply ,All i have to do is jus make a header file for that right and implent the label i need in the cpp file .I do not need to make a Cpp file for time label right ? just the label ?

Santosh Reddy
12th July 2013, 05:41
It's up to you, you could any way.

12th July 2013, 08:38
Ok i seem to have a problem . I still dont understand how to implement that .for the class timelabel..... it is suppose to be in a new header file right ?
I am not too sure how threading works or what , so the codes i dont understand at all.
what in tried to do is creating a new C++ class in the project . and i tried using the codes in a header file. I gotten errors

Added after 36 minutes:

Please ignore the previous reply. I already solved it . For now i am having troubles . I tried calling it in the main .cpp but it seems that it will open a new window to display the time and date . What i want to do is to display it one my gui , For eg. LoginGui.cpp, which doesnt have a main(), what must i do to display it on the gui ?

20th July 2013, 18:31
Create a QTimer instance in the class of yours, add a slot that you connect the timer to, make the slot update whatever widget you are using to display the time.
Start the timer with the desired update interval.