Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. function with argument in JS file called in my QML fail

function with argument in JS file called in my QML fail

Scheduled Pinned Locked Moved Solved 3rd Party Software
6 Posts 3 Posters 2.4k Views 2 Watching
  • 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.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by filipdns
    #1

    Hello guys,

    I'm trying to add argument to my JS fonction to use it in my QML form but it's not working.

    here working code:

    function sum()
    {
      var res = 0;
      var res1 = 0;
      var result = 0;
      for(var i = 0; i < listModel.count; i++){
      res += parseFloat(listModel.get(i).trip_time);
      }
      for(var i1 = 0; i1 < listView.currentRow; i1++){
      res1 += parseFloat(listModel.get(i1).trip_time);
      }
      result = res-res1;
      return result;
    }
    
    

    but when I try to replace "trip_time" by argument like that:

    function sum(column)
    {
      var res = 0;
      var res1 = 0;
      var result = 0;
      for(var i = 0; i < listModel.count; i++){
      res += parseFloat(listModel.get(i).column);
      }
      for(var i1 = 0; i1 < listView.currentRow; i1++){
      res1 += parseFloat(listModel.get(i1).column);
      }
      result = res-res1;
      return result;
    }
    

    and call the function in my qml with:

    JS.sum(trip_time)

    I receive error :
    ReferenceError: trip_time is not defined

    I used many time this method to add argument like below:

    function hourtodec(hour)
    {
        var res = hour.split(":")
        var hours = parseInt(res[0]);
        var minutes = parseInt(res[1]);
    
        var dec = hours+ minutes/60;
        return dec;
    }``
    
    and I never got this problem before...
    
    Could you help me please?
    
    Thank you very much
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If I understand correctly trip_time comes from your model. Where do you get it from when calling the new version ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F filipdns

        Hello guys,

        I'm trying to add argument to my JS fonction to use it in my QML form but it's not working.

        here working code:

        function sum()
        {
          var res = 0;
          var res1 = 0;
          var result = 0;
          for(var i = 0; i < listModel.count; i++){
          res += parseFloat(listModel.get(i).trip_time);
          }
          for(var i1 = 0; i1 < listView.currentRow; i1++){
          res1 += parseFloat(listModel.get(i1).trip_time);
          }
          result = res-res1;
          return result;
        }
        
        

        but when I try to replace "trip_time" by argument like that:

        function sum(column)
        {
          var res = 0;
          var res1 = 0;
          var result = 0;
          for(var i = 0; i < listModel.count; i++){
          res += parseFloat(listModel.get(i).column);
          }
          for(var i1 = 0; i1 < listView.currentRow; i1++){
          res1 += parseFloat(listModel.get(i1).column);
          }
          result = res-res1;
          return result;
        }
        

        and call the function in my qml with:

        JS.sum(trip_time)

        I receive error :
        ReferenceError: trip_time is not defined

        I used many time this method to add argument like below:

        function hourtodec(hour)
        {
            var res = hour.split(":")
            var hours = parseInt(res[0]);
            var minutes = parseInt(res[1]);
        
            var dec = hours+ minutes/60;
            return dec;
        }``
        
        and I never got this problem before...
        
        Could you help me please?
        
        Thank you very much
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @filipdns
        If I understand correctly:

        In your "working" example, hour is just a variable passed into function hourtodec, and is used as such.

        In your "non-working" code, you start with listModel.get(i).trip_time. trip_time is an attribute name. You cannot "factor this out" by trying to pass a parameter into sum() --- you can't simply define function as sum(column) and then use column in listModel.get(i1).column to somehow replace the need to reference the trip_time attribute.

        Unless QML --- which I know nothing about --- magically changes the syntax/behaviour of JavaScript....

        F 1 Reply Last reply
        2
        • F Offline
          F Offline
          filipdns
          wrote on last edited by
          #4

          Hello I solved the problem with :

          function sum(column)
          {
            var res = 0;
            var res1 = 0;
            var result = 0;
          for(var i = 0; i < listModel.count; i++){
            res += parseFloat(listModel.get(i)[column]);
            }
            for(var i1 = 0; i1 < listView.currentRow; i1++){
            res1 += parseFloat(listModel.get(i1)[column]);
            }
          result = res-res1;
            return result;
          }
          

          and call the function in my qml form with:

          JS.sum("trip-time")

          thank you for your help!!

          JonBJ 1 Reply Last reply
          1
          • JonBJ JonB

            @filipdns
            If I understand correctly:

            In your "working" example, hour is just a variable passed into function hourtodec, and is used as such.

            In your "non-working" code, you start with listModel.get(i).trip_time. trip_time is an attribute name. You cannot "factor this out" by trying to pass a parameter into sum() --- you can't simply define function as sum(column) and then use column in listModel.get(i1).column to somehow replace the need to reference the trip_time attribute.

            Unless QML --- which I know nothing about --- magically changes the syntax/behaviour of JavaScript....

            F Offline
            F Offline
            filipdns
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • F filipdns

              Hello I solved the problem with :

              function sum(column)
              {
                var res = 0;
                var res1 = 0;
                var result = 0;
              for(var i = 0; i < listModel.count; i++){
                res += parseFloat(listModel.get(i)[column]);
                }
                for(var i1 = 0; i1 < listView.currentRow; i1++){
                res1 += parseFloat(listModel.get(i1)[column]);
                }
              result = res-res1;
                return result;
              }
              

              and call the function in my qml form with:

              JS.sum("trip-time")

              thank you for your help!!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @filipdns
              Yes indeed, well done! Here instead of trying to use column as a direct JS property reference now you are using [column] as a property accessor and passing the string "trip_time" as the name of the property. In JS object.property refers to the same as object["property"].

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved