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 deal with the "time" basic type?
Qt 6.11 is out! See what's new in the release blog

How to deal with the "time" basic type?

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 5 Posters 7.6k 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.
  • Q Offline
    Q Offline
    qgil
    wrote on last edited by
    #1

    Hi, I need a counter that starting with a value e.g. "00:10:00" substracts a second every... second. ;)

    I thought this would work:

    @ Timer {
    property time userTimeValue: "00:10:00"
    interval: 1000; running: true; repeat: true
    onTriggered: {
    userTimeValue = userTimeValue - 1000
    userTime.text = userTimeValue
    }

        Text {
            id: userTime@
    

    but it can't even start counting because the "time" basic type is not recognized:

    bq. Error loading component: qrc:/OnlineBoard.qml:366 Expected property type

    The related documentation is a bit sparse, at least for a newbie like me: http://developer.qt.nokia.com/doc/qt-4.7/qml-time.html

    What is MyTimePicker? It seems to be mentioned only in that page.

    Also, once I succeed creating that property, will it be possible to make basic operations with it, like substracting 1000 miliseconds? Can I expect that after 60 seconds it will know that the new value returned has a minute less?

    And also, is there a way to present the value as "mm:ss" or "ss" or should I just cut the string using Javascript?

    By the way, I believe a countdown timer is a pretty frequent feature. Having it as an example would be welcome. I'm happy contributing mine once I get it to work. :)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Diph
      wrote on last edited by
      #2

      Check this example: http://www.developer.nokia.com/Community/Wiki/Simple_Qt_timer_application_in_QML

      Very ugly example. :)

      @import QtQuick 1.0

      Text {
      property int milliseconds: 1000 * 60 * 10 //ten minutes
      property string time: getTime()

      function getTime() {
          var minutes = Math.floor(milliseconds / 1000 / 60)
          var seconds = Math.floor((milliseconds-minutes*1000*60) / 1000)
      
          return "00:" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds)
      }
      
      width: 400
      height: 400
      text: time
      
      Timer  {
          interval: 1000; running: true; repeat: true;
          onTriggered: milliseconds -= 1000
      }
      

      }@

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qgil
        wrote on last edited by
        #3

        Maybe it's ugly but it does what I need, thanks! :)

        But you also had to circumvent the fact that "time" basic type seems to be cryptic even if it exists, right?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Diph
          wrote on last edited by
          #4

          The time basic type sounds weird (also date). There is Qt::formatTime(datetime time, variant format) which accepts Javascript Date and QTime and QDateTime and returns a formatted time string.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbrasser
            wrote on last edited by
            #5

            Hi,

            The "basic types" listed in the QML documentation are types that are encountered in QML (might be e.g. as a return value from a C++ function, for example), but not all of them can currently be declared as property types within QML (the list of supported types is much smaller, and is currently available at http://doc.qt.nokia.com/latest/qml-extending-types.html#supported-property-types). The documentation for this is not very clear at the moment, which unfortunately doesn't make this very easy to find out (hopefully this will be improved soon).

            Regards,
            Michael

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qgil
              wrote on last edited by
              #6

              Thanks mbrasser, this explanation makes total sense... now that I read it. :)

              You know, one problem with QML is that it's so easy to get started that even newbies like me (or your average designer) can get really far even ignoring the basics of e.g. Qt or Javascript (or programming for that matter). Until you hit one of these obvious things that average developers don't even pay attention.

              Looking forward to the improvements in the documentation to make it dummy-friendly.

              PS: thanks Diph, your simple timer found its place inside http://gitorious.org/miniature/miniature/blobs/master/src/frontend/OnlineBoard.qml - I still have to retouch it to handle the situation of reaching -1 milliseconds. :)

              1 Reply Last reply
              0
              • X Offline
                X Offline
                x89xfelipe
                wrote on last edited by
                #7

                Diph hi !
                when i use your code, qml show my this
                ReferenceError: milliseconds is not defined
                do you know why ? or the reason??

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="x89xfelipe" date="1404842956"]Diph hi !
                  when i use your code, qml show my this
                  ReferenceError: milliseconds is not defined
                  do you know why ? or the reason??[/quote]Because you didn't define "milliseconds" ;)

                  Anyway, just use JavaScript date/time functions: http://javascript.info/tutorial/datetime-functions Store your date/time object as a "var" type.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    x89xfelipe
                    wrote on last edited by
                    #9

                    You are very kind. thank you for your time ! :D

                    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