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. Binding signal and the function from another file
Forum Updated to NodeBB v4.3 + New Features

Binding signal and the function from another file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 753 Views 1 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.
  • AlexorleonA Offline
    AlexorleonA Offline
    Alexorleon
    wrote on last edited by
    #1

    Hi!
    I can't find, how to bind signal and the function from another file.

    main.qml

    PlayerForWindowPad {
            anchors.fill: parent
    }
    

    PlayerForWindowPad.qml

    RowLayout {
        id: rootLayout
        spacing: 1
    
        function foo1() {
            console.log("foo1")
        }
        function foo2() {
            console.log("foo2")
        }
        function foo3() {
            console.log("foo3")
        }
    
        PadButtonPForW {
            imageOn: "qrc:/images/outline-play.png"
            imageOff: "qrc:/images/outline-pause.png"
            // ????? action foo1
        }
        PadButtonPForW {
            imageOn: "qrc:/images/outline-stop.png"
            imageOff: "qrc:/images/outline-replay.png"
            // ????? action foo2
        }
        PadButtonPForW {
            imageOn: "qrc:/images/outline-audio-on.png"
            imageOff: "qrc:/images/outline-audio-off.png"
            // ????? action foo3
        }
    }
    

    PadButtonPForW.qml

    Item {
        id: root
        width: 40
        height: 40
    
        property string imageOn
        property string imageOff
    
        signal action
    
        Image {
            id: image
            anchors.fill: parent
            fillMode: Image.PreserveAspectFit
            source: imageOff
    
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: {
                    root.state = root.state == "on" ? "off" : "on"
                }
            }
        }
    
        states: [
            State {
                name: "on"
                PropertyChanges { target: image; source: imageOn }
            },
            State {
                name: "off"
                PropertyChanges { target: image; source: imageOff }
            }
        ]
    }
    

    Please help, how to pass a function to another file?

    kshegunovK 1 Reply Last reply
    0
    • AlexorleonA Alexorleon

      Hi!
      I can't find, how to bind signal and the function from another file.

      main.qml

      PlayerForWindowPad {
              anchors.fill: parent
      }
      

      PlayerForWindowPad.qml

      RowLayout {
          id: rootLayout
          spacing: 1
      
          function foo1() {
              console.log("foo1")
          }
          function foo2() {
              console.log("foo2")
          }
          function foo3() {
              console.log("foo3")
          }
      
          PadButtonPForW {
              imageOn: "qrc:/images/outline-play.png"
              imageOff: "qrc:/images/outline-pause.png"
              // ????? action foo1
          }
          PadButtonPForW {
              imageOn: "qrc:/images/outline-stop.png"
              imageOff: "qrc:/images/outline-replay.png"
              // ????? action foo2
          }
          PadButtonPForW {
              imageOn: "qrc:/images/outline-audio-on.png"
              imageOff: "qrc:/images/outline-audio-off.png"
              // ????? action foo3
          }
      }
      

      PadButtonPForW.qml

      Item {
          id: root
          width: 40
          height: 40
      
          property string imageOn
          property string imageOff
      
          signal action
      
          Image {
              id: image
              anchors.fill: parent
              fillMode: Image.PreserveAspectFit
              source: imageOff
      
              MouseArea {
                  id: mouseArea
                  anchors.fill: parent
                  onClicked: {
                      root.state = root.state == "on" ? "off" : "on"
                  }
              }
          }
      
          states: [
              State {
                  name: "on"
                  PropertyChanges { target: image; source: imageOn }
              },
              State {
                  name: "off"
                  PropertyChanges { target: image; source: imageOff }
              }
          ]
      }
      

      Please help, how to pass a function to another file?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2
          PadButtonPForW {
              imageOn: "qrc:/images/outline-audio-on.png"
              imageOff: "qrc:/images/outline-audio-off.png"
              onAction: {
                  // ... code ...
              }
          }
      

      But you never raise the action signal from your component. I'd assume the onClick handler should be modified like this:

      MouseArea {
          id: mouseArea
          anchors.fill: parent
          onClicked: {
              root.state = root.state == "on" ? "off" : "on"
              action(); // Emit the signal!
          }
      }
      

      Read and abide by the Qt Code of Conduct

      AlexorleonA 1 Reply Last reply
      1
      • kshegunovK kshegunov
            PadButtonPForW {
                imageOn: "qrc:/images/outline-audio-on.png"
                imageOff: "qrc:/images/outline-audio-off.png"
                onAction: {
                    // ... code ...
                }
            }
        

        But you never raise the action signal from your component. I'd assume the onClick handler should be modified like this:

        MouseArea {
            id: mouseArea
            anchors.fill: parent
            onClicked: {
                root.state = root.state == "on" ? "off" : "on"
                action(); // Emit the signal!
            }
        }
        
        AlexorleonA Offline
        AlexorleonA Offline
        Alexorleon
        wrote on last edited by
        #3

        @kshegunov, thank you so much! I thought I had already tried all the options) Yes, it is working!

        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