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

Detect Mouse Press&Hold Release



  • Hi All,
    i'm trying to detect the mouse press& hold as well as mouse press & hold release .
    But i'm unable to detect press& hold release event.

    Timer{
    id:qkSpinDownTimer
    interval: 200
    running: false
    repeat:true

      onTriggered: {
          console.log("Inside the timer Down of spinner")
          rotatedAntiClockwise(icompID,strType)
      }
    }
    
    
    
    
    Rectangle{
        id:qk_down_arrow
        width:100
        height:87
       // visible:false
        border.width:1
       // border.color:"#2a2a2a"
        color:"#616161"
        state:"ArrowStateACtive"
        anchors.top:qk_up_arrow.bottom
    

    // anchors.right:text1.right
    Image{
    width:30
    height:30
    anchors.centerIn:parent
    source: qkDownArrowImage
    }

        MouseArea{
            anchors.fill:parent
            onClicked: {
                console.log("Arrow Key down clicked \n");
                //state="ArrowStateSelected"
    
                rotateAntiClockwise(icompID,strType)
    
            }
            onPressAndHold: {
                console.log("on press and hold")
                qkSpinDownTimer.running=true;
                  qkSpinDownTimer.start();
            }
            onPressed: {
                parent.color="blue"
            }
    
            onReleased: {
                parent.color="gray"
    
                qkSpinDownTimer.running=false;
                  qkSpinDownTimer.stop();
            }
        }
    
    }


  • hi @Madesh-R

    for me I am able to detect press and hold, pressed and clicked

    import QtQuick 2.9
    import QtQuick.Window 2.2

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Rectangle
        {color:"RED"
    
            width:200
            height:200
            anchors.centerIn: parent
            MouseArea
            {
                anchors.fill:parent
                onClicked:
                {
                    console.log("clicked");
                }
                onPressed:
                {
                console.log("pressed")
                }
                onPressAndHold:
                {
                console.log("pressed and hold")
                }
    
            }
    
        }
    }
    

    output

    QML debugging is enabled. Only use this in a safe environment.
    qml: pressed
    qml: clicked
    qml: pressed
    qml: pressed and hold
    

    I think this is the thing you were asking for?



  • @ashajg

    Thanks for the reply.

    Let me state the problem very clearly.
    On press and hold of mouse button , i want to start the timer. And Should stop it when it is released.
    I'm getting the expected behaviour on doing separately for a single component.But with timers, I'm unable to do so.



  • ok I ll try with timers and let you know...



  • Thanks a lot for the help



  • hi
    try this
    I defined a timer here and on mouse pressandHold timer is getting started and prints a message and when mouse is released it will stop timer and printing of message will also stop.

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        
    
        Rectangle
        {
            width:100
            height:100
            color:"red"
            MouseArea
            {anchors.fill:parent
                onPressAndHold:
                {
                    ash.running=true
                }
                onReleased:
                {
                ash.running=false
                }
            }
        }
    
    
        Timer {id:ash
            interval: 500; running: false; repeat: true
            onTriggered: console.log("started")
        }
    }
    


  • @ashajg

    It certainly does what is required.Thank you. EVen i have tried the same. May be because of some other reasons i may not be qetting the required behaviour in the large chunk of code. Will look after that.



  • @Madesh-R Hi,
    just start your timer in onPressed and stop it by calling in onReleased

        onPressed: {
    
     qkSpinDownTimer.running=true;
                  qkSpinDownTimer.start();
    
            parent.color="blue"
        }
    
        onReleased: {
            parent.color="gray"
    
            qkSpinDownTimer.running=false;
              qkSpinDownTimer.stop();
        }
    
    After PressAndHold signal, onreleased signal never called.

Log in to reply