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. Button receiving clicked events even when behind a rectangle item and not visible in screen.

Button receiving clicked events even when behind a rectangle item and not visible in screen.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qtquick controlbuttonevent propogaticlickeventoverlap
3 Posts 2 Posters 2.0k 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.
  • A Offline
    A Offline
    Ashvit
    wrote on 9 Oct 2020, 13:04 last edited by
    #1

    Simplified example

    
    import QtQuick 2.12
    import QtQuick.Controls 1.0
    import QtQuick.Window 2.12
    
    
    Window {
        id: window
        visible: true
        width: 100
        height: 100
        color: "Black"
    
        Rectangle{
            id:rect1
            z:100
            anchors.top: parent
            anchors.left: parent
            width: 45
            height: 35
            color:"red"
        }
        Rectangle{
            id:rect2
            color: "green"
            anchors.fill: parent
            Button{
                id:btn1
                anchors.left: parent.left
                text: "Button1"
                onClicked: {
                    console.log("Button1 clicked");
                }
            }
        }
    }
    

    In the above example the red rectangle is above the part of a button, see z-axis, the problem I am facing is when I click the red rectangle in the overlapped portion where button is behind the rectangle, the button gets the click event(Log message "Button1 clicked").

    I have the following requirements.

    • I need to keep the button behind enabled, as the red rectangle is the result of a transition and it may move later based on user interaction.
    • The portion inside the red rectangle can have multiple controls, so adding a mouse area inside the red rectangle and using propogateCompositeEvents through it wont work in my case.
    • I don't want the button to receive any event when I click anywhere inside the red rectangle.

    Is there any way I can achieve my requirements?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 9 Oct 2020, 15:11 last edited by
      #2

      Does this do what you want?

      import QtQuick 2.12
      import QtQuick.Controls 1.0
      import QtQuick.Window 2.12
      
      
      Window {
          id: window
          visible: true
          width: 100
          height: 100
          color: "Black"
      
          Rectangle{
              id:rect2
              color: "green"
              anchors.fill: parent
              Button{
                  id:btn1
                  anchors.left: parent.left
                  text: "Button1"
                  onClicked: {
                      console.log("Button1 clicked");
                  }
              }
          }
      
          MouseArea {
              id: eat_events_mousearea
              anchors.fill: rect1
          }
      
          Rectangle{
              id: rect1
              //z:100 // you can use creation order to control z
              anchors.top: parent
              anchors.left: parent
              width: 45
              height: 35
              color:"red"
      
              // all controls here take precedence over eat_events_mousearea, again using creation order to control z
          }
      }
      

      C++ is a perfectly valid school of magic.

      A 1 Reply Last reply 12 Oct 2020, 05:06
      2
      • F fcarney
        9 Oct 2020, 15:11

        Does this do what you want?

        import QtQuick 2.12
        import QtQuick.Controls 1.0
        import QtQuick.Window 2.12
        
        
        Window {
            id: window
            visible: true
            width: 100
            height: 100
            color: "Black"
        
            Rectangle{
                id:rect2
                color: "green"
                anchors.fill: parent
                Button{
                    id:btn1
                    anchors.left: parent.left
                    text: "Button1"
                    onClicked: {
                        console.log("Button1 clicked");
                    }
                }
            }
        
            MouseArea {
                id: eat_events_mousearea
                anchors.fill: rect1
            }
        
            Rectangle{
                id: rect1
                //z:100 // you can use creation order to control z
                anchors.top: parent
                anchors.left: parent
                width: 45
                height: 35
                color:"red"
        
                // all controls here take precedence over eat_events_mousearea, again using creation order to control z
            }
        }
        
        A Offline
        A Offline
        Ashvit
        wrote on 12 Oct 2020, 05:06 last edited by
        #3

        @fcarney Thankyou for the prompt reply, your solution does solve my problem.

        1 Reply Last reply
        1

        1/3

        9 Oct 2020, 13:04

        • Login

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