问题
I'd like to open a context menu when a user presses and holds a Button (I use Button for convenience). If I do
Button
{
text: model.ualabel
MouseArea
{
preventStealing: true
anchors.fill: parent
onPressAndHold: uaContextMenu.open()
}
ContextMenu
{
id: uaContextMenu
MenuLayout
{
MenuItem { /**/ }
}
}
}
then the MouseArea responsible for pressAndHold steals all gestures even though and the Button cannot be clicked. What am I doing wrong? I'm using Qt 4.7 and importing QtQuick 1.1 and com.nokia.meego 1.0
Thanks
回答1:
I've found that press-and-hold can be simulated on QML's Button like this:
Button {
id: button
signal pressAndHold()
Timer {
id: longPressTimer
interval: 2000 //your press-and-hold interval here
repeat: false
running: false
onTriggered: {
button.pressAndHold()
}
}
onPressedChanged: {
if ( pressed ) {
longPressTimer.running = true;
} else {
longPressTimer.running = false;
}
}
}
回答2:
The problem is your mouse area is competing with the Button's mouse area to receive mouse events. Try setting propogateComposedEvents: true on your mouse area and this should allow event to propogate downwards in the visual stack to the button's mouse area. Refer to http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#propagateComposedEvents-prop for more details.
After reading the comments, my new suggestion is to manually propagate the clicked signal in your mouseArea to the button. This should be doable by calling buttonId.clicked() which will manually emit the clicked signal on your button.
来源:https://stackoverflow.com/questions/17857570/detect-press-and-hold-of-a-button-in-qml