Is it possible to simulate right or left mouse button clicks on a QGIS map canvas. Currently writing tests for a plugin and need to be able to create a line geometry thorough the use of theses buttons.

Have been trying...

QTest.mouseClick(canvas, Qt.RightButton, pos=QPoint(1200,500), delay=1000)  
  • ...and then? What happens if you try this? – Jochen Schwarze Jul 28 '17 at 5:16
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. You appear to have created two accounts which inevitably leads to a frustrating experience for you, potential answerers and reviewers so please follow these instructions ASAP to merge your accounts. – PolyGeo Jul 28 '17 at 5:33

Perhaps this related post might help: https://stackoverflow.com/questions/16299779/qt-qgraphicsview-unit-testing-how-to-keep-the-mouse-in-a-pressed-state

It seems you have to pass the mapCanvasses viewport to the QTest like

widget = iface.mapCanvas().viewport()
QTest.mouseClick(widget, Qt.RightButton, pos=QPoint(1200,500), delay=1000)

I just tried this, and it works (QGIS, select 'Select Features by area or single click' Tool), note the selected parcel:

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.