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. How to call function from parent QML (feedback) ?
Forum Updated to NodeBB v4.3 + New Features

How to call function from parent QML (feedback) ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 2.8k 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.
  • R Offline
    R Offline
    r3d9u11
    wrote on 9 Aug 2019, 06:44 last edited by r3d9u11 8 Sept 2019, 06:49
    #1

    Hi. I have two different QMLs main.qml and Child.qml.
    main.qml contains some objects/functions which I want to call from Child.qml.
    For example:

    main.qml:

    Window {
        ....
        Child {
            id: myChildQml
            width: ...
            height: ...
            ...
            fnStart: function () { myTimer.start(); }
        }
        ...
        Timer {
            id: myTimer
            interval: 30
            repeat: true
            running: false
            triggeredOnStart: true
            onTriggered: {
            ...
            }
        }
    }
    

    Child.qml:

    Rectangle {
            id: root
    
            color: "#a1b2c3"
            opacity: 0.8
            anchors.fill: parent
    
            property variant fnStart;
    
            Button {
                id: button
                x: 19
                y: 21
                width: 118
                height: 57
                text: qsTr("Start")
            }
    
            Connections {
                target: button
                onClicked: {
                    root.fnStart;
                }
            }
    }
    

    I tried to pass timer.start via custom property fnStart, but it doesn't work.

    (sorry if this theme is duplicated, but I didn't find any solution, yet)

    Is it possible to realize something like that?
    Any ideas?

    Thanks!

    P 1 Reply Last reply 9 Aug 2019, 06:49
    0
    • R r3d9u11
      9 Aug 2019, 06:44

      Hi. I have two different QMLs main.qml and Child.qml.
      main.qml contains some objects/functions which I want to call from Child.qml.
      For example:

      main.qml:

      Window {
          ....
          Child {
              id: myChildQml
              width: ...
              height: ...
              ...
              fnStart: function () { myTimer.start(); }
          }
          ...
          Timer {
              id: myTimer
              interval: 30
              repeat: true
              running: false
              triggeredOnStart: true
              onTriggered: {
              ...
              }
          }
      }
      

      Child.qml:

      Rectangle {
              id: root
      
              color: "#a1b2c3"
              opacity: 0.8
              anchors.fill: parent
      
              property variant fnStart;
      
              Button {
                  id: button
                  x: 19
                  y: 21
                  width: 118
                  height: 57
                  text: qsTr("Start")
              }
      
              Connections {
                  target: button
                  onClicked: {
                      root.fnStart;
                  }
              }
      }
      

      I tried to pass timer.start via custom property fnStart, but it doesn't work.

      (sorry if this theme is duplicated, but I didn't find any solution, yet)

      Is it possible to realize something like that?
      Any ideas?

      Thanks!

      P Offline
      P Offline
      Pradeep P N
      wrote on 9 Aug 2019, 06:49 last edited by Pradeep P N 8 Sept 2019, 06:54
      #2

      @r3d9u11

      Please refer Signal and Handler Event System.

      • Child.qml
      Rectangle {
              id: root
      
              color: "#a1b2c3"
              opacity: 0.8
              anchors.fill: parent
      
              signal startFunction()
              
             Button {
                  id: button
                  x: 19
                  y: 21
                  width: 118
                  height: 57
                  text: qsTr("Start")
      
               onClicked: {
                    root.startFunction();
               }
              }
      }
      
      • main.qml
      Child {
              id: myChildQml
              width: ...
              height: ...
              ...
              onSrartFunction: {
                                          // Please start the timer here.
              }
          }
      

      All the best.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      4
      • R Offline
        R Offline
        r3d9u11
        wrote on 9 Aug 2019, 07:07 last edited by
        #3

        Many thanks for useful link to a conception and code example, it works!

        P 1 Reply Last reply 9 Aug 2019, 09:55
        0
        • R r3d9u11
          9 Aug 2019, 07:07

          Many thanks for useful link to a conception and code example, it works!

          P Offline
          P Offline
          Pradeep P N
          wrote on 9 Aug 2019, 09:55 last edited by
          #4

          @r3d9u11

          Cool :)
          All the best.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          1 Reply Last reply
          0

          1/4

          9 Aug 2019, 06:44

          • Login

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