Not possible to reset a `date` property?
-
I understand the proper way to reset a qt property in qml is by assigning
undefinedto it. This does not seem possible with thedatetype. I need to do this both for properties defined in qml and in my c++ models which appropriately use theQDateTimetype.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 QDateTimeThe evidence suggests that there are only two states for a
dateproperty: 1)Invalid Date- no idea what value that implies though it is the default, 2) a validDateobject.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()?
-
I understand the proper way to reset a qt property in qml is by assigning
undefinedto it. This does not seem possible with thedatetype. I need to do this both for properties defined in qml and in my c++ models which appropriately use theQDateTimetype.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 QDateTimeThe evidence suggests that there are only two states for a
dateproperty: 1)Invalid Date- no idea what value that implies though it is the default, 2) a validDateobject.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()?
@patrickkidd
I haven't tested it, but my guess would bemyDate = new Date() -
@patrickkidd
I haven't tested it, but my guess would bemyDate = new Date()@J.Hilk said in Not possible to reset a `date` property?:
@patrickkidd
I haven't tested it, but my guess would bemyDate = new Date()Unfortunately, constructing a new
Dateobject sets it to today's date, at least in the implementation included in qml. -
@J.Hilk said in Not possible to reset a `date` property?:
@patrickkidd
I haven't tested it, but my guess would bemyDate = new Date()Unfortunately, constructing a new
Dateobject sets it to today's date, at least in the implementation included in qml.@patrickkidd
I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....In JS
Dateobject has no "invalid" state.new Date()always sets to date now. In QtQDate::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
dateproperty". But if, for whatever reason, it gives you that error aboutundefined, have you at least triedmyDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes? -
@patrickkidd
I don't know anything about QML, but I do know about JavaScript. So don't know if this helps, but....In JS
Dateobject has no "invalid" state.new Date()always sets to date now. In QtQDate::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
dateproperty". But if, for whatever reason, it gives you that error aboutundefined, have you at least triedmyDate = null, which is valid in JS and maybe is acceptable to QML, whatever that is doing behind the scenes?@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
Dateobject has no "invalid" state.new Date()always sets to date now. In QtQDate::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
dateproperty". But if, for whatever reason, it gives you that error aboutundefined, have you at least triedmyDate = 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
undefinedto 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 assigningundefinedto properties defined in qml with thedatetype seems to act weird. -
@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
Dateobject has no "invalid" state.new Date()always sets to date now. In QtQDate::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
dateproperty". But if, for whatever reason, it gives you that error aboutundefined, have you at least triedmyDate = 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
undefinedto 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 assigningundefinedto properties defined in qml with thedatetype seems to act weird.@patrickkidd
And trying assigningnullinstead of the rejectedundefineddoes not hack it? -
@patrickkidd
And trying assigningnullinstead of the rejectedundefineddoes not hack it?@JonB said in Not possible to reset a `date` property?:
@patrickkidd
And trying assigningnullinstead of the rejectedundefineddoes not hack it?Doesn't work. You get the same error but for
nullptr. -
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 doqmlDate = invalidDateproperty 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 -
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 doqmlDate = invalidDateproperty 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@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 doqmlDate = invalidDateproperty 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 DateThat 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++
QDateTimeproperty from qml. -
If you have a property with a RESET function, it will be called if you assign
undefinedto it.I tested it with a C++ class exposed to QML.
-
If you have a property with a RESET function, it will be called if you assign
undefinedto it.I tested it with a C++ class exposed to QML.
@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
undefinedto it.I tested it with a C++ class exposed to QML.
I figured out the right way to do this. I used a
varproperty instead of adateproperty. , which doesn't perform the Qml-side type checking and properly passes theundefinedvalue back to the model in myonDateChangedhandler: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 } } }