4

QML provides in its MouseArea component a PressAndHold signal, when a mouse area is pressed for a "long duration" http://doc.qt.io/qt-5/qml-qtquick-mousearea.html#pressAndHold-signal

this duration is set to 800ms, and I find nowhere a way to modify this duration. Can it be done and if so, how can I do that?

Thanks!

3 Answers 3

8

If you will see the MouseArea source (Src/qtdeclarative/src/quick/items/qquickmousearea.cpp) you find this line:

d->pressAndHoldTimer.start(qApp->styleHints()->mousePressAndHoldInterval(), this);

The durations value came from QStyleHints but it's read-only since the value is platform specified. So the answer to your question: "No", if you are not going to change the source.

But you still can emulate this events, for example:

MouseArea {
    property int pressAndHoldDuration: 2000
    signal myPressAndHold()
    anchors.fill: parent
    onPressed: {
        pressAndHoldTimer.start();
    }
    onReleased: {
        pressAndHoldTimer.stop();
    }
    onMyPressAndHold: {
        console.log("It works!");
    }

    Timer {
        id:  pressAndHoldTimer
        interval: parent.pressAndHoldDuration
        running: false
        repeat: false
        onTriggered: {
            parent.myPressAndHold();
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You can simplify that by setting the running property of the Timer to parent.pressed and get rid of the onPressed and onReleased handlers.
The funny thing is, even by using private APIs, i.e. QT += gui_private, there is no direct setMousePressAndHoldInterval available (at least I didn't find it). I guess this property is "The Devil" :D
Is there a way to cancel onReleased event ? I've implemented a similar solution, but I would like to have myPressAndHold INSTEAD of the onReleased signa.
5

Yes, this can be directly configured with setMousePressAndHoldInterval() (added in November 2015), e.g.:

int pressAndHoldInterval = 2000; // in [ms]
QGuiApplication::styleHints()->setMousePressAndHoldInterval(pressAndHoldInterval);

Put the above at the beginning in your main(), along with

#include <QStyleHints>

and it will globally set the interval as desired.

NOTE #1: As per the Qt bug report, this is a system-wide configuration, so individual MouseArea components cannot be fine-tuned.

NOTE #2: In the source code, the doxygen lists this as \internal so this might be removed/refactored without warning.

Comments

5

Since Qt 5.9, the property pressAndHoldInterval overrides the elapsed time in milliseconds before pressAndHold is emitted.

Documentation

import QtQuick 2.9 // 2.9 or higher

MouseArea {
    pressAndHoldInterval: 100 // duration of 100ms
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.