Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved setHours() do not work

    QML and Qt Quick
    javascript date qml
    2
    3
    2060
    Loading More Posts
    • 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.
    • A
      Alart last edited by Alart

      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.

      1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by

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

        1 Reply Last reply Reply Quote 0
        • A
          Alart last edited by Alart

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post