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 exclude the transparent area to receive mouse events under MouseArea element?
Qt 6.11 is out! See what's new in the release blog

How to exclude the transparent area to receive mouse events under MouseArea element?

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

    I have a image with transparent background. I want define the MouseArea that will include only the non-transparent part of the image.
    How to do that? Currently it is taking the complete rectangle (including the transparent area) that hods the image.

    Below is my code snippet inside Rectangle { } element:

    @Image
    {
    id: image1
    anchors.left: parent.left
    anchors.leftMargin: 20
    anchors.verticalCenter: parent.verticalCenter
    source: "images/launch.png"
    scale: 0.4
    smooth: true

    MouseArea
    {
        id: mouseArea1
        anchors.fill: parent
        hoverEnabled: true
        onPressed: console.log("mouse pressed\n")
    }
    

    }

    Text
    {
    text: "Launch"
    font.pixelSize: 18
    anchors.left: image1.right
    anchors.verticalCenter: parent.verticalCenter
    color: mouseArea1.containsMouse ? "light blue" : "white"
    }@

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup

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

      I'm afraid you need to check the colour under the cursor every time a click is made. At least I don't know any other solution.

      (Z(:^

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jens
        wrote on last edited by
        #3

        There is no simple way to filter mouse events by image data. The only option apart from using a custom C++ Item, would be to draw the image with a Canvas element and use the getData function to get the alpha value/transparency of a pixel on each mousePress.

        I would however suggest that you simply approximate the clickable button area by setting some appropriate anchor margins on the mouse Area as it is good enough in most normal use cases.

        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