Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. javascript function in QML
Forum Update on Monday, May 27th 2025

javascript function in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmljavascript
3 Posts 2 Posters 1.3k Views
  • 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 Offline
    A Offline
    ansh
    wrote on 22 Nov 2015, 07:36 last edited by ansh
    #1

    Why I'm not getting the desired result in the following code?

    Item {
        id: root
        width: 200
        height: 200
    
        function foo() {
            var l = 2+2;
        }
    
        Component.onCompleted: {
            var str = foo.toString();
            console.log(str)
        }
    }
    

    It's giving me function() { [code] } rather then function foo() { var l = 2+2; } .What's the reason for such behaviour?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kambiz
      wrote on 22 Nov 2015, 15:41 last edited by
      #2

      Because you must use return in your function.
      Of course not need to convert function to String, check blow code :)

      Item {
              id: root
              width: 200
              height: 200
      
              function foo() {
                  var l = 2 + 2;
                  return l;
              }
      
              Component.onCompleted: {
                  console.log(foo())
              }
          }
      
      

      .:.We speak a universal language that brings us together.:.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ansh
        wrote on 22 Nov 2015, 16:23 last edited by
        #3

        My desired output is function foo() { var l = 2+2; }.

        1 Reply Last reply
        0

        3/3

        22 Nov 2015, 16:23

        • Login

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