Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How display Time (hh:mm:ss) not statically?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 12.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    enaud84
    wrote on last edited by
    #1

    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:

    http://time.is/it/

    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 : http://time.is/it/???

    thanks for help

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by
        #3

        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).

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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! :-)

          1 Reply Last reply
          0
          • E Offline
            E Offline
            enaud84
            wrote on last edited by
            #5

            thanks,
            how connect this:
            @
            QTimer *timer = new QTimer(this);
            connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
            timer->start(1000);
            @
            at my label?

            Edit: please use @ tags around code sections; Andre

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                enaud84
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  You're welcome, have fun learning Qt!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved