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 implement Interaction between two QML Files?
Qt 6.11 is out! See what's new in the release blog

How to implement Interaction between two QML Files?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 1.0k 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by
    #1

    OnLeftDockerExpand can respond to the leftDockerExpand signal from sendId in QML.

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Layouts 1.1
    
    Item{
         id:sendId
          signal leftDockerExpand
          Rectangle{
                   id:rctId 
                   anchors.fill:parent
    
                   MouseArea {
                           anchors.fill: parent
                           acceptedButtons: Qt.LeftButton | Qt.RightButton
                   
                           onClicked: {           
                               if (mouse.button == Qt.RightButton){
                                       sendId.leftDockerExpand()                                
                              }
                        }
                }
          }
    }
    
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Layouts 1.1
    
    Item{
         id:recvId
         OnLeftDockerExpand{
                   console.log("sendId")
          }
    }
    
    ODБOïO 1 Reply Last reply
    0
    • M mirro

      OnLeftDockerExpand can respond to the leftDockerExpand signal from sendId in QML.

      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Layouts 1.1
      
      Item{
           id:sendId
            signal leftDockerExpand
            Rectangle{
                     id:rctId 
                     anchors.fill:parent
      
                     MouseArea {
                             anchors.fill: parent
                             acceptedButtons: Qt.LeftButton | Qt.RightButton
                     
                             onClicked: {           
                                 if (mouse.button == Qt.RightButton){
                                         sendId.leftDockerExpand()                                
                                }
                          }
                  }
            }
      }
      
      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Layouts 1.1
      
      Item{
           id:recvId
           OnLeftDockerExpand{
                     console.log("sendId")
            }
      }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      hi
      @mirro said in How to implement Interaction between two QML Files?:

      How to implement Interaction between two QML Files?

      You don't implement interaction between files but between Componets/objects.
      In a QML (.qml) file you describe a Component

      // QmlComponent_A.qml
      Item {
          signal mySignal()
      
          Button{
              text: "emit signal"
              onClicked: mySignal()
          }
      }
      
      
      // QmlComponent_B.qml
      import QtQuick 2.12
      
      Item {
       function react(){
          console.log("reaction")
       }
      }
      
      

      Then create the component, and implement the interaction, for example with Connections type

      //main.qml
      Window {
          id: appWindow
          width: 512
          height: 512
          visible: true
      
          Connections{
              target: a
              function onMySignal(){
                  b.react()
              }
          }
      
          QmlComponent_A{
              id:a
          }
      
          QmlComponent_B{
              id:b
          }
      
      }
      
      

      https://doc.qt.io/qt-5/qml-tutorial2.html

      1 Reply Last reply
      3
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        Note that the Connections in the last example is not needed here. It is only needed when you want to react to an object's signal in a different context that its instantiation.

        Here you could do:

        QmlComponent_A{
            onMySignal: b.react()
        }
        
        QmlComponent_B{
            id:b
        }
        
        1 Reply Last reply
        3

        • Login

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