8768

Detect press and hold of a Button in QML

Question:

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

Answer1:

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; } } }

Answer2:

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 <a href="http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#propagateComposedEvents-prop" rel="nofollow">http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#propagateComposedEvents-prop</a> 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.

Recommend

  • Pinch and zoom behaviour for windows phone
  • nsattributedstring tappable iOS
  • Arranging multiple objects in a circle QML
  • Scroll tableview cell in cube effect in iOS?
  • Gestures and color drawing
  • UICollectionView Enable deselecting cells while allowsMultipleSelection is disabled
  • How to set the default button of a MessageDialog in QML?
  • GCC 2.9 and “lvalue required as left operand of assignment”
  • How to capture video stream using emgu cv
  • QQuickView: handling mouse events in C++
  • Compiling Crypto++ for armhf for cross compiling
  • Getting wrong UIButton tag on Long press gesture recognizer
  • How to invoke ActionListener for MenuItem in primefaces
  • GotoBLAS error when installing matplotlib with pip in a virtualenv on debian wheezy
  • Form Post with enctype = “multipart/form-data” causing parameters to not get passed
  • Binding ContextMenu Tag to Owner
  • Git and client/server code separation
  • Importing matplotlib on Ubuntu
  • QObject::findChild() returns None without obvious reason
  • How to import Navit into Eclipse and use it in own android project
  • Different Datacontext for Command and CommandParameter
  • Cross compiling Qt 5
  • How to remove comma or any characters from Python dataframe column name
  • How to give custom name to Sqoop output files
  • CoreData basics – to-many relationship array data
  • PyQt4 application on Windows is crashing on exit
  • change color of jstree node
  • Google analytics measurement protocol session timeout and query time limits
  • Blackberry 6: how to detect a long click on track pad?
  • Wrong row deleted from custom listview with spinner
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Specify the x-axis values in ggplot in R
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • Detect when Facebook like button is clicked
  • Meteor: Do Something On Email Verification Confirmation
  • Where to put my custom functions in Wordpress?
  • RestKit - RKRequestDelegate does not exist
  • WPF Applying a trigger on binding failure