setHours() do not work



  • Hello, that's my very first post here.
    Let me skip to the point. In my qml file setHours() function, (part of date prototype) does not work. However, getters do work. Here is my code:

    var new_time = (calendarForDate.chosen_Hour.getMinutes() + 1)%60
    console.log(new_time) // new_time is calculated properly
    calendarForDate.chosen_Hour.setMinutes(new_time)
    console.log(calendarForDate.chosen_Hour.getMinutes()) // now it's the same as before anyway
    

    My question - I don't know...
    Is there something wrong with that?
    Is that a bug?
    I'm using qtcreator under KDE Mint but it's installation directly from qt, not Ubuntu SDK. I'm more c++ man, so I'm not sure.



  • Hi! Can you create a complete minimal working example qml file?



  • Luckily I found solution myself :)
    According to http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions "global" object cannot be modified inside a JavaScript function.
    In this cause that code was part of onClicked property.
    Proper code:

    onClicked : {
          var working_time = calendarForDate.chosen_Hour; // local variable
          working_time.setHours( (calendarForDate.chosen_Hour.getHours() + 1)%24 ); // now JavaScript can modify that
          calendarForDate.chosen_Hour = working_time;
          console.log(calendarForDate.chosen_Hour.getHours()); // one cannot modify global object, but that's perfectly fine
          hoursField.text = calendarForDate.chosen_Hour.getHours() // now it works as intended
    }
    

    Thanks for you attention.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.