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 can event handler in one component access object in another component?
Qt 6.11 is out! See what's new in the release blog

How can event handler in one component access object in another component?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 720 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by
    #1

    Within a single qml file, how can an event handler invoked by one component access an object defined in another component? E.g. in this pseudocode, the onPressed mouse event handler within MouseArea wants to access an object a slider object contained within another Rectangle:

    Rectangle {
       MouseArea {
         onPressed: 
           // Mouse was pressed - reset slider scale
           mySlider.to = 20  // ReferenceError: mySlider is not defined
         }
       }
    }
     
    Rectangle {
       Slider {
         id: mySlider
       }
    }
    

    But the code in the MouseArea onPressed event handler cannot see the mySlider object. I've read the Scope and Naming Resolution document but it isn't clear to me exactly how I do this.

    Thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #4

      You can simplify it like this:

       property alias sliderValue: mySlider.to
       
       Rectangle {
          MouseArea {
            onPressed: 
              // Mouse was pressed - reset slider scale
              sliderValue.to = 20   // Referring to the property works
            }
          }
       }
        
       Rectangle {
          Slider {
            id: mySlider
          }
       }
      

      (Z(:^

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        So they are both in the same file? That should work. The only exception is if your mouse are is within a delegate - then you have to relay the signal through an intermediate property in your view component.

        (Z(:^

        Tom assoT 1 Reply Last reply
        1
        • sierdzioS sierdzio

          So they are both in the same file? That should work. The only exception is if your mouse are is within a delegate - then you have to relay the signal through an intermediate property in your view component.

          Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by
          #3

          @sierdzio - thanks! Yes, the QML that assigns the id and the code that references the id is in a single file. I've never encountered the concept of a "delegate" - I've read this link, and I do not think my qml contains a delegate. My "hack" is to define a property which I assign to the slider id when the slider is created, and the MouseArea onPressed signal handler refers to that property:

          property var theSlider: -1
          
          Rectangle {
             MouseArea {
               onPressed: 
                 // Mouse was pressed - reset slider scale
                 // mySlider.to = 20  // ReferenceError: mySlider is not defined
                 theSlider.to = 20   // Referring to the property works
               }
             }
          }
           
          Rectangle {
             Slider {
               id: mySlider
          
               Component.onCompleted: {
                  // Slider created - assign its id to property
                   theSlider = mySlider;
               }
             }
          }
          

          So I've got a workaround, but I don't know why it's needed!

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by sierdzio
            #4

            You can simplify it like this:

             property alias sliderValue: mySlider.to
             
             Rectangle {
                MouseArea {
                  onPressed: 
                    // Mouse was pressed - reset slider scale
                    sliderValue.to = 20   // Referring to the property works
                  }
                }
             }
              
             Rectangle {
                Slider {
                  id: mySlider
                }
             }
            

            (Z(:^

            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