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 propagate hover events when using MouseArea?
Forum Updated to NodeBB v4.3 + New Features

How to propagate hover events when using MouseArea?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
mouseareahovermouse eventevent handling
4 Posts 2 Posters 51 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.
  • bibasmallB Offline
    bibasmallB Offline
    bibasmall
    wrote last edited by
    #1

    Hello everyone! I've got Mouse Area blocking hover events for the underlying element. I need to keep it on top, so the question is how to propagate hover events.
    Setting hoverEnabled: false does nothing.

    import QtQuick 2.15
    
    MouseArea {
        id: root
    
        property point previousPosition: defaultPosition
    
        readonly property point defaultPosition: Qt.point(-1, -1)
    
        signal mousePositionChanged(point position, point previousPosition)
    
        onPressed:  previousPosition = Qt.point(mouseX, mouseY)
        onReleased: previousPosition = defaultPosition
    
        onPositionChanged:
        {
            if (pressed) {
                mousePositionChanged(Qt.point(mouseX, mouseY), previousPosition)
            } else {
                mouse.accepted = false
            }
        }
    
        hoverEnabled: true
        propagateComposedEvents: true
    }
    

    How do I implement this?

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

      Could you use a PointHandler instead of the MouseArea?

      bibasmallB 1 Reply Last reply
      2
      • GrecKoG GrecKo

        Could you use a PointHandler instead of the MouseArea?

        bibasmallB Offline
        bibasmallB Offline
        bibasmall
        wrote last edited by
        #3

        @GrecKo
        Thank you so much! It solved the problem. This is how I implemented it:

        import QtQuick 2.15
        
        Item {
            id: root
        
            property point previousPosition: defaultPosition
        
            readonly property point defaultPosition: Qt.point(-1, -1)
            readonly property bool hovered: hoverHandler.hovered
        
            property alias hoverEnabled: hoverHandler.enabled
        
            signal mousePositionChanged(point position, point previousPosition)
            signal pressed()
            signal released()
            signal doubleClicked()
        
            HoverHandler {
                id: hoverHandler
                acceptedDevices: PointerDevice.Mouse
            }
        
            PointHandler {
                id: clickHandler
        
                onActiveChanged:
                {
                    if (active) {
                        root.previousPosition = Qt.point(point.position.x, point.position.y)
                        root.pressed()
                    } else {
                        root.previousPosition = root.defaultPosition
                        root.released()
                    }
                    point.accepted = false
                }
        
                onPointChanged:
                {
                    if (active) {
                        root.mousePositionChanged(Qt.point(point.position.x, point.position.y), root.previousPosition)
                    }
                }
        
                acceptedButtons: Qt.LeftButton
            }
        
            TapHandler {
                id: doubleClickHandler
        
                onTapped:
                {
                    if (tapCount === 2) {
                        root.doubleClicked()
                    }
                }
        
                acceptedDevices: PointerDevice.Mouse
                acceptedButtons: Qt.LeftButton
            }
        }
        
        1 Reply Last reply
        0
        • bibasmallB bibasmall has marked this topic as solved
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote last edited by
          #4

          Nice, note that TapHandler has a doubleTapped signal.

          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