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. Not possible to reset a `date` property?

Not possible to reset a `date` property?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 2.3k 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by patrickkidd
    #1

    I understand the proper way to reset a qt property in qml is by assigning undefined to it. This does not seem possible with the date type. I need to do this both for properties defined in qml and in my c++ models which appropriately use the QDateTime type.

    Here is a simple example:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    import QtQuick.Layouts 1.12                                                                                                                                                           
    
    ColumnLayout {
    
        property date myDate
    
        Button {
            text: 'Reset'
            onClicked: {
                print('Default value:', myDate)
                myDate = undefined
                print('After reset attempt:', myDate)
            }
        }
    }
    

    which prints the following output:

    qml: Default value: Invalid Date
    file:///Users/patrick/dev/pkdiagram/test.qml:14: Error: Cannot assign [undefined] to QDateTime
    

    The evidence suggests that there are only two states for a date property: 1) Invalid Date - no idea what value that implies though it is the default, 2) a valid Date object.

    Then what is the reset state and how do you achieve it? It seems like that default ought to be something similar to a null QDateTime()?

    https://alaskafamilysystems.com/

    J.HilkJ 1 Reply Last reply
    0
    • P patrickkidd

      I understand the proper way to reset a qt property in qml is by assigning undefined to it. This does not seem possible with the date type. I need to do this both for properties defined in qml and in my c++ models which appropriately use the QDateTime type.

      Here is a simple example:

      import QtQuick 2.12
      import QtQuick.Controls 2.5
      import QtQuick.Layouts 1.12                                                                                                                                                           
      
      ColumnLayout {
      
          property date myDate
      
          Button {
              text: 'Reset'
              onClicked: {
                  print('Default value:', myDate)
                  myDate = undefined
                  print('After reset attempt:', myDate)
              }
          }
      }
      

      which prints the following output:

      qml: Default value: Invalid Date
      file:///Users/patrick/dev/pkdiagram/test.qml:14: Error: Cannot assign [undefined] to QDateTime
      

      The evidence suggests that there are only two states for a date property: 1) Invalid Date - no idea what value that implies though it is the default, 2) a valid Date object.

      Then what is the reset state and how do you achieve it? It seems like that default ought to be something similar to a null QDateTime()?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @patrickkidd
      I haven't tested it, but my guess would be

      myDate = new Date()
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      P 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @patrickkidd
        I haven't tested it, but my guess would be

        myDate = new Date()
        
        P Offline
        P Offline
        patrickkidd
        wrote on last edited by
        #3

        @J.Hilk said in Not possible to reset a `date` property?:

        @patrickkidd
        I haven't tested it, but my guess would be

        myDate = new Date()
        

        Unfortunately, constructing a new Date object sets it to today's date, at least in the implementation included in qml.

        https://alaskafamilysystems.com/

        JonBJ 1 Reply Last reply
        0
        • P patrickkidd

          @J.Hilk said in Not possible to reset a `date` property?:

          @patrickkidd
          I haven't tested it, but my guess would be

          myDate = new Date()
          

          Unfortunately, constructing a new Date object sets it to today's date, at least in the implementation included in qml.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @patrickkidd
          I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....

          In JS Date object has no "invalid" state. new Date() always sets to date now. In Qt QDate::QDate() "Constructs a null date. Null dates are invalid.".

          I don't know how you marry those two, as they are incompatible, and I don't really know why you need "to reset a date property". But if, for whatever reason, it gives you that error about undefined, have you at least tried myDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes?

          P 1 Reply Last reply
          1
          • JonBJ JonB

            @patrickkidd
            I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....

            In JS Date object has no "invalid" state. new Date() always sets to date now. In Qt QDate::QDate() "Constructs a null date. Null dates are invalid.".

            I don't know how you marry those two, as they are incompatible, and I don't really know why you need "to reset a date property". But if, for whatever reason, it gives you that error about undefined, have you at least tried myDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes?

            P Offline
            P Offline
            patrickkidd
            wrote on last edited by
            #5

            @JonB said in Not possible to reset a `date` property?:

            @patrickkidd
            I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....

            In JS Date object has no "invalid" state. new Date() always sets to date now. In Qt QDate::QDate() "Constructs a null date. Null dates are invalid.".

            I don't know how you marry those two, as they are incompatible, and I don't really know why you need "to reset a date property". But if, for whatever reason, it gives you that error about undefined, have you at least tried myDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes?

            Yes, this question pertains quite closely to Qt and Qml as opposed to ECMAScript. JS Date objects are automatically converted to QDateTime when passed to Qt properties in C++. Qt properties have a reset function, and so an arbitrary reset state. Assigning undefined to a Qt property via Qml triggers a special condition where it calls the reset function on the property. So my question pertains to the fact that assigning undefined to properties defined in qml with the date type seems to act weird.

            https://alaskafamilysystems.com/

            JonBJ 1 Reply Last reply
            0
            • P patrickkidd

              @JonB said in Not possible to reset a `date` property?:

              @patrickkidd
              I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....

              In JS Date object has no "invalid" state. new Date() always sets to date now. In Qt QDate::QDate() "Constructs a null date. Null dates are invalid.".

              I don't know how you marry those two, as they are incompatible, and I don't really know why you need "to reset a date property". But if, for whatever reason, it gives you that error about undefined, have you at least tried myDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes?

              Yes, this question pertains quite closely to Qt and Qml as opposed to ECMAScript. JS Date objects are automatically converted to QDateTime when passed to Qt properties in C++. Qt properties have a reset function, and so an arbitrary reset state. Assigning undefined to a Qt property via Qml triggers a special condition where it calls the reset function on the property. So my question pertains to the fact that assigning undefined to properties defined in qml with the date type seems to act weird.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @patrickkidd
              And trying assigning null instead of the rejected undefined does not hack it?

              P 1 Reply Last reply
              0
              • JonBJ JonB

                @patrickkidd
                And trying assigning null instead of the rejected undefined does not hack it?

                P Offline
                P Offline
                patrickkidd
                wrote on last edited by
                #7

                @JonB said in Not possible to reset a `date` property?:

                @patrickkidd
                And trying assigning null instead of the rejected undefined does not hack it?

                Doesn't work. You get the same error but for nullptr.

                https://alaskafamilysystems.com/

                1 Reply Last reply
                1
                • GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on last edited by
                  #8

                  A workaround could be to set an invalid QDateTime as a contextProperty and assign your date to that.

                  engine.rootContext()->setContextProperty("invalidDate", QDateTime());
                  You can then do qmlDate = invalidDate

                  property date qmlDate
                  
                  Component.onCompleted: {
                      print(qmlDate);
                      qmlDate = new Date();
                      print(qmlDate);
                      qmlDate = invalidDate;
                      print(qmlDate);
                  }
                  

                  This outputs:

                  qml: Invalid Date
                  qml: mer. juil. 17 11:57:49 2019 GMT+0200
                  qml: Invalid Date
                  
                  P 1 Reply Last reply
                  3
                  • GrecKoG GrecKo

                    A workaround could be to set an invalid QDateTime as a contextProperty and assign your date to that.

                    engine.rootContext()->setContextProperty("invalidDate", QDateTime());
                    You can then do qmlDate = invalidDate

                    property date qmlDate
                    
                    Component.onCompleted: {
                        print(qmlDate);
                        qmlDate = new Date();
                        print(qmlDate);
                        qmlDate = invalidDate;
                        print(qmlDate);
                    }
                    

                    This outputs:

                    qml: Invalid Date
                    qml: mer. juil. 17 11:57:49 2019 GMT+0200
                    qml: Invalid Date
                    
                    P Offline
                    P Offline
                    patrickkidd
                    wrote on last edited by
                    #9

                    @GrecKo said in Not possible to reset a `date` property?:

                    A workaround could be to set an invalid QDateTime as a contextProperty and assign your date to that.

                    engine.rootContext()->setContextProperty("invalidDate", QDateTime());
                    You can then do qmlDate = invalidDate

                    property date qmlDate
                    
                    Component.onCompleted: {
                        print(qmlDate);
                        qmlDate = new Date();
                        print(qmlDate);
                        qmlDate = invalidDate;
                        print(qmlDate);
                    }
                    

                    This outputs:

                    qml: Invalid Date
                    qml: mer. juil. 17 11:57:49 2019 GMT+0200
                    qml: Invalid Date
                    

                    That is a pretty cool idea. The only thing it leaves out is the final goal of my question, which is to find a way to call the reset function on my C++ QDateTime property from qml.

                    https://alaskafamilysystems.com/

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #10

                      If you have a property with a RESET function, it will be called if you assign undefined to it.

                      I tested it with a C++ class exposed to QML.

                      P 1 Reply Last reply
                      1
                      • GrecKoG GrecKo

                        If you have a property with a RESET function, it will be called if you assign undefined to it.

                        I tested it with a C++ class exposed to QML.

                        P Offline
                        P Offline
                        patrickkidd
                        wrote on last edited by patrickkidd
                        #11

                        @GrecKo said in Not possible to reset a `date` property?:

                        If you have a property with a RESET function, it will be called if you assign undefined to it.

                        I tested it with a C++ class exposed to QML.

                        I figured out the right way to do this. I used a var property instead of a date property. , which doesn't perform the Qml-side type checking and properly passes the undefined value back to the model in my onDateChanged handler:

                        Something like this:

                        ColumnLayout {
                            property var date
                            property var model: null // set in C++
                            onDateChanged: {
                                if(model)
                                    model.date = date // calls C++ `date` custom reset function designated with Q_PROPERTY when `date` is undefined
                            }
                            Button {
                                text: 'Set to today'
                                onClicked: {
                                    date = new Date
                                }
                            }
                            Button {
                                text: 'Reset property'
                                onClicked: {
                                    date = undefined // no type checking now
                                }
                            }
                        }
                        

                        https://alaskafamilysystems.com/

                        1 Reply Last reply
                        1

                        • Login

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