Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to change the current time with Text

How to change the current time with Text

Scheduled Pinned Locked Moved QML and Qt Quick
25 Posts 7 Posters 13.9k Views
  • 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.
  • D Offline
    D Offline
    disperso
    wrote on last edited by
    #1

    Hi.

    This is one of the things that it's probably not completely straightforward to do in QML. Imagine that you want to display the current time, including the seconds, in QML. If you do:

    @
    import QtQuick 1.0

    Rectangle {
    id: main
    width: 400; height: 400

    Text {
        id: foo
        font.pointSize: 24
        text: Date();
    }
    

    }
    @

    Then the "text" value is not a binding, so the time is not updated each second, of course.

    Any idea about how to solve it cleanly? I've tried using the Binding element, and qmlviewer doesn't throw any errors, but reading the docs doesn't seem the proper system anyway.

    I thought of using a timer, but doesn't seem the proper way to do it with QML.

    Any ideas?
    Thank you.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcrochik
      wrote on last edited by
      #2

      I can't imagine there is any other way than using a timer. If you were to try something like this on c++ you probably would end up with a timer the same way... somehow you need to decide how often you want to update the time (it is always changing...)

      Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

      1 Reply Last reply
      0
      • D Offline
        D Offline
        disperso
        wrote on last edited by
        #3

        Yes, I suppose that there is no way to scape of the timer. A case like this can't be designed declaratively because the time is always changing, it it doesn't emit a signal all the time. :(

        So, it's possible to set the timer in the QML code? Or just the C++?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcrochik
          wrote on last edited by
          #4

          what about using javascript?

          Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

          1 Reply Last reply
          0
          • D Offline
            D Offline
            disperso
            wrote on last edited by
            #5

            But how would you set it up? I mean, in C++ I would add a line in the constructor of the widget. In QML...

            Did I say that I'm quite a noob with this? :)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kamalakshantv
              wrote on last edited by
              #6

              Will this work for you?

              @import QtQuick 1.0

              Rectangle {
              id: main
              width: 400; height: 400

              Text {
                  id: foo
                  font.pointSize: 12
                function set()
               {
                      foo.text = Date();
                   } 
              }
              

              Timer {
              id: textTimer
              interval: 1000
              repeat: true
              running: true
              triggeredOnStart: true
              onTriggered: {
              foo.set();
              }
              }
              }
              @

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fcrochik
                wrote on last edited by
                #7

                or that.... ;)

                Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kamalakshantv
                  wrote on last edited by
                  #8

                  [quote author="disperso" date="1292182667"]But how would you set it up? I mean, in C++ I would add a line in the constructor of the widget. In QML...

                  Did I say that I'm quite a noob with this? :)[/quote]

                  QML is new so everyone is a noob. :)

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fcrochik
                    wrote on last edited by
                    #9

                    [quote author="QtK" date="1292183966"]
                    [quote author="disperso" date="1292182667"]But how would you set it up? I mean, in C++ I would add a line in the constructor of the widget. In QML...

                    Did I say that I'm quite a noob with this? :)[/quote]

                    QML is new so everyone is a noob. :)

                    [/quote]

                    Of course there are different levels of noob... :)

                    On a more serious note: it is quite amazing to me how many posts we are getting about qml... I have to say that I didn't think would be such a hit!

                    Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      baysmith
                      wrote on last edited by
                      #10

                      QML Timer seems the proper QML way to do it to me. :)

                      I'd forgotten about the Timer element myself. One of the problems with this new way of developing UIs is remember what can and can't be done in C++ vs QML.

                      Nokia Certified Qt Specialist.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        disperso
                        wrote on last edited by
                        #11

                        Oh my, I didn't knew that a Timer existed in QML. This makes a lot of sense. Thank you!

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fcrochik
                          wrote on last edited by
                          #12

                          I wonder if there isn't a way using an animation. I know it is overkill but it is another idea...

                          Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kamalakshantv
                            wrote on last edited by
                            #13

                            [quote author="fcrochik" date="1292185303"]I wonder if there isn't a way using an animation. I know it is overkill but it is another idea...[/quote]

                            Yes, I think you can animation too. I had tried it for other values but not with date. But then yes it would be a overkill.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              disperso
                              wrote on last edited by
                              #14

                              Well, an animation that triggers each second and changes the second with some eye candy is quite a stress test for the app, isn't it?

                              I modified once "digiflip, frmo the graphics dojo":http://qt.gitorious.org/qt-labs/graphics-dojo/trees/03c1e210d0a54f7337dd953a10b66108c54ed827/digiflip to change each second, and it consumed quite a lot of CPU... and it's done in C++ (and only does that :P).

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                kamalakshantv
                                wrote on last edited by
                                #15

                                I assume you tested it on a PC or Laptop. On mobile device it might freeze.

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  baysmith
                                  wrote on last edited by
                                  #16

                                  Updating every second shouldn't stress the CPU since updates need to be every 16 ms to get smooth animations. Of course whether smooth animations are possible depends upon what is animating and the system animating it. I wouldn't expect updating text to be one of those cases.

                                  Nokia Certified Qt Specialist.

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    kamalakshantv
                                    wrote on last edited by
                                    #17

                                    Just another concern I have about QML application is the initial loading time it takes on Mobile Devices especially on higher ends like N97. Native Symbian applications load a lot more quickly. Hope this would be optimized in someway in future.

                                    1 Reply Last reply
                                    0
                                    • X Offline
                                      X Offline
                                      xsacha
                                      wrote on last edited by
                                      #18

                                      Updating something every second should definitely not affect CPU very much. Even on a mobile device. A small timer is very simple and not stressful. I have done much more wicked things in my QML code :P All done on CPU on a 400MHz device of course.

                                      It takes roughly 1-2 seconds to load a QML app here. Noting that the QML app includes images that are not hinted to load asynchronously. 1-2 seconds may be slower than native Symbian apps but it's still faster than apps on most platforms including iOS, Android, etc.. so I don't think there's much problem. Just need that loading process animated.

                                      • Sacha
                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        kamalakshantv
                                        wrote on last edited by
                                        #19

                                        For eg the flowd demo you had referred to takes more time than symbian apps to load. But once loaded it works fine.

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          disperso
                                          wrote on last edited by
                                          #20

                                          [quote author="xsacha" date="1292193076"]Updating something every second should definitely not affect CPU very much. Even on a mobile device. A small timer is very simple and not stressful. I have done much more wicked things in my QML code :P All done on CPU on a 400MHz device of course.
                                          [/quote]

                                          The digiflip demo that I mentioned runs smooth, but I have a widget that show the CPU usage, and if I made the numbers too big (or just not small), it goes up easily. As I said, it's c++, but honestly, it's code that I don't dare to understand deeply (I mean, the paint() stuff, the rest is easy of course).

                                          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