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. Use a function in qml from another qml
QtWS25 Last Chance

Use a function in qml from another qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 11.5k 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.
  • P Offline
    P Offline
    petzold
    wrote on 17 Sept 2014, 12:11 last edited by
    #1

    Hello,

    i want to use an function in qml what is written in an other qml file, but it won't work
    the example
    the function is written in the file "function.qml" in an seperate directory called components
    the function looks like this
    function assign(id) {
    value = id + 20
    return: value;
    }

    now i work in another qml file it looks like that.

    import "components/"

    property int test: function.assign(number)

    if i now run the qml I got an message in the qml-log "ReferenceError: Can't find variable: function"

    i don't understand it. have someone an idea?
    if i wrote the function in the same file it work.
    can i call a function like this?

    NOTE: this is just an example for the function

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 17 Sept 2014, 13:49 last edited by
      #2

      welcome to forum. Did you create the instance of second qml component inside the first qml ? You need create instance, assign the id and through id you need to call.

      Try this.
      @=== TestFunction.qml====
      Rectangle {
      width: 100
      height: 100
      function callme(val, val){
      console.log("I am called man="+val + " ok="+val)
      }
      }
      ====== main.qml========
      Rectangle {
      width: 640
      height: 480

      TestFunction{
          id : func
      }
      
      Button {
          text: qsTr("Hello World")
          anchors.horizontalCenter: parent.horizontalCenter
          anchors.verticalCenter: parent.verticalCenter
          onClicked: {
              func.callme("pthinks.com","Bangaluru")
          }
      }
      

      }
      @

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      R 1 Reply Last reply 7 Feb 2017, 07:01
      6
      • P Offline
        P Offline
        petzold
        wrote on 17 Sept 2014, 16:23 last edited by
        #3

        thanks works fine.
        should be something simple like this.
        [SOLVED]

        1 Reply Last reply
        1
        • D dheerendra
          17 Sept 2014, 13:49

          welcome to forum. Did you create the instance of second qml component inside the first qml ? You need create instance, assign the id and through id you need to call.

          Try this.
          @=== TestFunction.qml====
          Rectangle {
          width: 100
          height: 100
          function callme(val, val){
          console.log("I am called man="+val + " ok="+val)
          }
          }
          ====== main.qml========
          Rectangle {
          width: 640
          height: 480

          TestFunction{
              id : func
          }
          
          Button {
              text: qsTr("Hello World")
              anchors.horizontalCenter: parent.horizontalCenter
              anchors.verticalCenter: parent.verticalCenter
              onClicked: {
                  func.callme("pthinks.com","Bangaluru")
              }
          }
          

          }
          @

          R Offline
          R Offline
          Raghvendra
          wrote on 7 Feb 2017, 07:01 last edited by
          #4

          @dheerendra great dheerendra it worked for me Thanks a lot :)

          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