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
Forum Updated to NodeBB v4.3 + New Features

function with argument in JS file called in my QML fail

Scheduled Pinned Locked Moved Solved 3rd Party Software
6 Posts 3 Posters 1.9k 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 4 Jan 2018, 14:43 last edited by filipdns 1 Apr 2018, 14:44
    #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
    J 1 Reply Last reply 4 Jan 2018, 22:41
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Jan 2018, 18:14 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
        4 Jan 2018, 14:43

        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
        J Online
        J Online
        JonB
        wrote on 4 Jan 2018, 22:41 last edited by JonB 1 Apr 2018, 22:44
        #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 5 Jan 2018, 07:33
        2
        • F Offline
          F Offline
          filipdns
          wrote on 5 Jan 2018, 07:32 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!!

          J 1 Reply Last reply 5 Jan 2018, 08:03
          1
          • J JonB
            4 Jan 2018, 22:41

            @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 5 Jan 2018, 07:33 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • F filipdns
              5 Jan 2018, 07:32

              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!!

              J Online
              J Online
              JonB
              wrote on 5 Jan 2018, 08:03 last edited by JonB 1 May 2018, 08:03
              #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

              1/6

              4 Jan 2018, 14:43

              • Login

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