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. Using Date.fromLocalTimeString in property binding
Forum Updated to NodeBB v4.3 + New Features

Using Date.fromLocalTimeString in property binding

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.9k 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.
  • P Offline
    P Offline
    Panke
    wrote on last edited by
    #1

    I've got a Q_GADGET called Incident with a QDateTime property dateTime. I want to edit this property using an QtQiuck TextField, thus I've got something like this:

    Item {
    property variant incident
       TextField {
                id: time
                placeholderText: "Time"
                text: .incident.dateTime.toLocaleTimeString().slice(0,5)
                validator: RegExpValidator { regExp: /\d\d:\d\d/ }
            }
     Binding {
            target: incident.dateTime
            when: time.acceptableInput
            value: {
                var d = incident.dateTime;
                var t = Date.fromLocalTimeString(Qt.locale(), time.text, "hh:mm");
                d.setMinutes(t.getMinutes());
                d.setHours(t.getHours());
                return d
            }
    }
    

    What I am trying to do is to read keep the value in the incident-property and the text field in sync, also I only want to change the hour and the minutes of the dateTime object. However when I try to call the fromLocalTimeString method, I get the following error message:

    TypeError: Property 'fromLocalTimeString' of object function() { [code] } is not a function

    How do I call this function correctly and is the basic approach correct?

    raven-worxR 1 Reply Last reply
    0
    • P Panke

      I've got a Q_GADGET called Incident with a QDateTime property dateTime. I want to edit this property using an QtQiuck TextField, thus I've got something like this:

      Item {
      property variant incident
         TextField {
                  id: time
                  placeholderText: "Time"
                  text: .incident.dateTime.toLocaleTimeString().slice(0,5)
                  validator: RegExpValidator { regExp: /\d\d:\d\d/ }
              }
       Binding {
              target: incident.dateTime
              when: time.acceptableInput
              value: {
                  var d = incident.dateTime;
                  var t = Date.fromLocalTimeString(Qt.locale(), time.text, "hh:mm");
                  d.setMinutes(t.getMinutes());
                  d.setHours(t.getHours());
                  return d
              }
      }
      

      What I am trying to do is to read keep the value in the incident-property and the text field in sync, also I only want to change the hour and the minutes of the dateTime object. However when I try to call the fromLocalTimeString method, I get the following error message:

      TypeError: Property 'fromLocalTimeString' of object function() { [code] } is not a function

      How do I call this function correctly and is the basic approach correct?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Panke said in Using Date.fromLocalTimeString in property binding:

      See the QML Date docs:

      The QML Date object extends the JS Date object with locale aware functions.

      This leads to:

      Binding {
              target: incident.dateTime
              when: time.acceptableInput
              value: {
                  var d = incident.dateTime;
                  // <<<<<<<<<<<<<<<<<<<<<<<<
                  var date = new Date();
                  var t = date.fromLocalTimeString(Qt.locale(), time.text, "hh:mm");
                  // >>>>>>>>>>>>>>>>>>>>
                  d.setMinutes(t.getMinutes());
                  d.setHours(t.getHours());
                  return d
              }
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      P 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Panke said in Using Date.fromLocalTimeString in property binding:

        See the QML Date docs:

        The QML Date object extends the JS Date object with locale aware functions.

        This leads to:

        Binding {
                target: incident.dateTime
                when: time.acceptableInput
                value: {
                    var d = incident.dateTime;
                    // <<<<<<<<<<<<<<<<<<<<<<<<
                    var date = new Date();
                    var t = date.fromLocalTimeString(Qt.locale(), time.text, "hh:mm");
                    // >>>>>>>>>>>>>>>>>>>>
                    d.setMinutes(t.getMinutes());
                    d.setHours(t.getHours());
                    return d
                }
        }
        
        P Offline
        P Offline
        Panke
        wrote on last edited by
        #3

        @raven-worx Thanks.

        From the linked documentation page:

        import QtQml 2.0
        
        QtObject {
            property var locale: Qt.locale()
            property string dateTimeString: "Tue 2013-09-17 10:56:06"
        
            Component.onCompleted: {
                print(Date.fromLocaleString(locale, dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));
            }
        }
        
        Is the documentation wrong or is there something I don't get?
        
        raven-worxR 2 Replies Last reply
        0
        • P Panke

          @raven-worx Thanks.

          From the linked documentation page:

          import QtQml 2.0
          
          QtObject {
              property var locale: Qt.locale()
              property string dateTimeString: "Tue 2013-09-17 10:56:06"
          
              Component.onCompleted: {
                  print(Date.fromLocaleString(locale, dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));
              }
          }
          
          Is the documentation wrong or is there something I don't get?
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Panke
          it's not impossible that the docs are wrong and IMHO they are in this case.
          At least all the other lines in the docs refering to toLocaleTimeString() are all constructing a new Data instance.

          But honestly i have no clue why the return types of those methods are string?
          When the text says that it converts to a Date object.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • P Panke

            @raven-worx Thanks.

            From the linked documentation page:

            import QtQml 2.0
            
            QtObject {
                property var locale: Qt.locale()
                property string dateTimeString: "Tue 2013-09-17 10:56:06"
            
                Component.onCompleted: {
                    print(Date.fromLocaleString(locale, dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));
                }
            }
            
            Is the documentation wrong or is there something I don't get?
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @Panke
            But i also noticed a typo in your code:
            You wrote fromLocalTimeString instead of fromLocaleTimeString (note the e at the end of Locale)
            If thats the issue the docs are correct afterall ;)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panke
              wrote on last edited by
              #6

              You wrote fromLocalTimeString instead of fromLocaleTimeString (note the e at the end of Locale)

              That actually solves the issue. Thank you. Didn't look close enough thinking that on object function() { [code] } couldn't have such a property anyway.

              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