PDA

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



020394
11th July 2013, 11: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, 12:05
One quick way


class TimeLabel : public QLabel
{
Q_OBJECT
public:
explicit TimeLabel(QWidget * parent = 0)
: QLabel(parent)
{
startTimer(50); // Update time every 50 ms
}
protected:
void timerEvent(QTimerEvent *)
{
setText(QDateTime::currentDateTime().toString());
}
};

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

TimeLabel label;
label.show();

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

020394
12th July 2013, 01: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, 04:41
It's up to you, you could any way.

020394
12th July 2013, 07: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 ?

anda_skoa
20th July 2013, 17: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.

Cheers,
_