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 detect if left mouse button is pressed in mouseArea in onPositionChanged?
Forum Updated to NodeBB v4.3 + New Features

How to detect if left mouse button is pressed in mouseArea in onPositionChanged?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.2k 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.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    Hi,

    I have a mouseArea with hover enabled.

    In the handler "onPositionChanged" I would like to detect if e.g. the left mouse button is pressed or not.

    Somehow this is not working.
    Could you please give me a hint?

    Thanks!

    MWE:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Rectangle {
            width: 400; height: 400
            anchors.centerIn: parent
            color: "green"
    
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
    
                onPositionChanged: {
                    console.log(mouseX, mouseY)
                    if(mouse.button == Qt.LeftButton)  // Does not detect a pressed left mouse button! Why?
                        console.log("Left pressed")
                    else console.log("Left button not detected!")
                }
            }
        }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • R robro

      Hi,

      I have a mouseArea with hover enabled.

      In the handler "onPositionChanged" I would like to detect if e.g. the left mouse button is pressed or not.

      Somehow this is not working.
      Could you please give me a hint?

      Thanks!

      MWE:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Rectangle {
              width: 400; height: 400
              anchors.centerIn: parent
              color: "green"
      
              MouseArea {
                  anchors.fill: parent
                  hoverEnabled: true
      
                  onPositionChanged: {
                      console.log(mouseX, mouseY)
                      if(mouse.button == Qt.LeftButton)  // Does not detect a pressed left mouse button! Why?
                          console.log("Left pressed")
                      else console.log("Left button not detected!")
                  }
              }
          }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @robro there are 2 properties you can check:

      for any accepted Button to be pressed:
      https://doc.qt.io/qt-5/qml-qtquick-mousearea.html#pressed-prop

      To know what exact button was pressed:
      https://doc.qt.io/qt-5/qml-qtquick-mousearea.html#pressedButtons-prop


      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
      1
      • R Offline
        R Offline
        robro
        wrote on last edited by
        #3

        Thanks!
        The second suggestion works fine!

        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