Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Mouse event for MouseArea



  • Is there anyway to detect an over all event in a MouseArea? For instance, I have a MouseArea, and I am performing actions at each event such as onClicked or onPressed, but I want to fire a signal if either onPressed or onClicked happens without putting the signal in code on both events, is that possible?


  • Moderators

    @RobM said in Mouse event for MouseArea:

    Is there anyway to detect an over all event in a MouseArea? For instance, I have a MouseArea, and I am performing actions at each event such as onClicked or onPressed, but I want to fire a signal if either onPressed or onClicked happens without putting the signal in code on both events, is that possible?

    Yes, use a function:

    MouseArea {
      function doSomething() {
        // Something
      }
    
      onClicked: doSomething()
      onPressed: doSomething()
    }
    


  • I should have been more specific. Here is what I have:

    signal mouseEvent
    
    MouseArea
    {
      onPressed: do stuff
      onReleased: do stuff
      onClicked: do stuff
      onPressAndHold: do stuff
      onPositionChanged: do stuff
      onWheel: do stuff
    }
    

    so in order to trigger mouseEvent I would have to add it to each event like so:

    signal mouseEvent
    
    MouseArea
    {
      onPressed: mouseEvent()
      onReleased: mouseEvent()
      onClicked: mouseEvent()
      onPressAndHold: mouseEvent()
      onPositionChanged: mouseEvent()
      onWheel: mouseEvent()
    }
    

    but, I was hoping I might be able to avoid that somehow, for instance:

    signal mouseEvent: mouseArea.event //<-- or something like this "event" isn't real... I made it up, but I was hoping for something like that.
    MouseArea
    {
      id: mouseArea
    
      onPressed: do stuff
      onReleased: do stuff
      onClicked: do stuff
      onPressAndHold: do stuff
      onPositionChanged: do stuff
      onWheel: do stuff
    }
    

    but, I can't think of a way. If I could do it this way the I could reduce the calls to mouseEvent to one instead of (in this instance) 6.



  • @RobM
    You could abstract it:
    Put in AllEventMouseArea.qml

    MouseArea {
      property var allfunc
      onPressed: allfunc()
      onReleased: allfunc()
      onClicked: allfunc()
      onPressAndHold: allfunc()
      onPositionChanged: allfunc()
      onWheel: allfunc()
    }
    

    Then in other code:

    AllEventMouseArea {
      func: {
        // do stuff
      }
    }
    

Log in to reply