How display Time (hh:mm:ss) not statically?

  • Hi all,
    I'm studing QT only for fun..
    i would like to build a little application with a label wich shows the exact time that flows like this:

    I writed:
    ui->label->setText(QTime::currentTime().toString("h:mm:ss "));

    but I get only the time instant, and the second does not run ..
    the time is still!

    how can I correctly display the time? like this :

    thanks for help

  • Put that line in a function, and make that function a slot. Now, from your constructor, create a [[doc:QTimer]] instance, and connect the timeout() signal of the timer you just created with the function that updates the time in the label. Voila!

  • I don't think the text of a QLabel is meant to automatically change by itself. The QTime::toString gives the QString corrsponding to the input QTime value. I guess if you want a QLabel that changes, you'll have to use signal/slots to rewrite your QLabel every second (i.e. every time the QTime changes).

  • Off-topic: Note that in QML, such a setup would work: tell a component to display the current time, and it will always display the current time, even while the time changes. Declarative programming is cool that way! :-)

  • thanks,
    how connect this:
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    at my label?

    Edit: please use @ tags around code sections; Andre

  • Your method showTime() needs to contain the line you posted in your first message, and the function needs to be declared a slot in your header.

  • i've resolved with a new function! thanks ANDRE!!!!!

  • You're welcome, have fun learning Qt!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.