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. MouseArea disabled if item on top
Qt 6.11 is out! See what's new in the release blog

MouseArea disabled if item on top

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

    Hello,

    I have a question regarding MouseArea and other items.
    Is it possible to disable MouseArea if something is on top of it ?
    For example:

    Item{
       id: mainItem
       height: 400
       width: 800
       MouseArea{
          anchors.fil: parent
          onClicked: console.log("Hello !")
       }
       Rectangle{
          z: 2
          anchors.fill: parent
          color: "blue"
       }
    }
    

    In this example, the rectangle is on top of the mouseArea so I would like to disable mouseArea events, is it possible ?

    Thank you

    J.HilkJ 1 Reply Last reply
    0
    • D DeltaSim

      Hello,

      I have a question regarding MouseArea and other items.
      Is it possible to disable MouseArea if something is on top of it ?
      For example:

      Item{
         id: mainItem
         height: 400
         width: 800
         MouseArea{
            anchors.fil: parent
            onClicked: console.log("Hello !")
         }
         Rectangle{
            z: 2
            anchors.fill: parent
            color: "blue"
         }
      }
      

      In this example, the rectangle is on top of the mouseArea so I would like to disable mouseArea events, is it possible ?

      Thank you

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @DeltaSim certainly

      but there are many ways to do this.

      The easiest, that comes to my mind right now is, simply give your rectangle an mouseArea of its own, that will capture the events (by default) and not pass it on to the mouseArea underneath


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        @J-Hilk's method is the best I know of for completely declarative syntax simplicity.

        Here's a partially imperative solution using Item.childAt():

        Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: parent.childAt(mouseX, mouseY) === this ?
                    console.log("not covered") :
                    console.log("covered") 
            }
            Rectangle {
                width: parent.width / 2
                height: width
                color: "blue"
            }
        }
        

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        2
        • D Offline
          D Offline
          DeltaSim
          wrote on last edited by
          #4

          Thanks for your feedbacks @J-Hilk and @jeremy_k .
          Your solutions seem good, I will pick on of the two.

          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