Using Date.fromLocalTimeString in property binding
-
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?
-
@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 } }
-
@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?
-
@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 totoLocaleTimeString()
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. -
@Panke
But i also noticed a typo in your code:
You wrotefromLocalTimeString
instead offromLocaleTimeString
(note the e at the end of Locale)
If thats the issue the docs are correct afterall ;) -