Solved new Date() in Qml Js function return a TypeError
-
Hello,
I do have implement a function to get the date and time every second but once I reach the new Date() call I get a TypeError.
Here is the code I run :
Timer { id: timer interval: 1000 repeat: true running: true onTriggered: { getDate() } } function getDate(){ var today = new Date() //Here is the line throwing the error var date = Qt.formatDate(today, "dd/MM/yyyy") var dateTime = date labelOptionDate = dateTime }
Here is the error :
qrc:/OptionDate.qml:84: TypeError: Type error
-
Provided example working fine for me:
Timer { id: timer interval: 1000 repeat: true running: true onTriggered: getDate() } function getDate() { console.log("Requesting date"); var date = new Date(); }
I can see call of the function every second. Qt 5.12.3.
-
@DavidM29 said in new Date() in Qml Js function return a TypeError:
labelOptionDate
Can you tell me what is
labelOptionDate
in your code ?
And from where you are accessing it ? -
labelOptionDate is a Text component in my .qml file that I update with the value but I don't reach it at this point. I'm having an error before that.
-
hi
@DavidM29 said in new Date() in Qml Js function return a TypeError:labelOptionDate is a Text component in my .qml
then there is a type error here
labelOptionDate = dateTime
should be
labelOptionDate.text = dateTime
i often do this mistake
-
@LeLev
Sorry I did not explain properly.
It is a property that I use to set the Text of a Text component...
The code I'm running used to work on another computer and still running on another one...property string labelOptionDate: getDate() Text { id: textOption text: labelOptionDate // Here is my labelOptionDate use color: Style.textColor font.family: localFont.name font.pixelSize: Style.globalTextSize anchors.verticalCenter: parent.verticalCenter }
-
ok
@DavidM29 said in new Date() in Qml Js function return a TypeError:property string labelOptionDate: getDate()
this means labelOptionDate is = to the value returned by getDate(), but it returns nothing
try
property string labelOptionDate: "" // don't call the function here, let this empty , the function called by the timer will update this every second
-
@LeLev
Sorry for the delay...
I don't know why but the day after the problem was gone... Maybe my clean and rebuild was not completely made...But anyways @LeLev I took your remarks in consideration and changed my code in that way.
Thank you for the help
-
@DavidM29
Perfect!
Make sure you mark this as SOLVED topic