4
votes

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

3 Answers

8
votes

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();
        }
    }
}
5
votes

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.

3
votes

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
}