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

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 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
    • KambizK Offline
      KambizK Offline
      Kambiz
      wrote on 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 last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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