3

I have a python plugin for QGIS where I manage to show a list annotations auto created by the plugin.

Meanwhile, I can display the annotations in a combobox, on user selection the annotation object is selected on the canvas (and this is done already)

I need to open the annotation text edit window for the selected annotation .. how can I do that .. can anyone guide me to a proper solution ?

Annotation text edit window

1 Answer 1

4

That's not possible using QGIS API v2.x (or even the current API v3.x), since those edit dialogs and the tool that opens them are not exposed (i.e., they are part of the QGIS application itself).

However, as a workaround you could simulate a double click event on the selected annotation and get their corresponding edit dialog opened.

You could reuse the following custom function I've created for that purpose:

from qgis.gui import QgsTextAnnotationItem
from PyQt4.QtCore import Qt, QEvent
from PyQt4.QtGui import QApplication, QMouseEvent

def editSelectedAnnotation():
    selectedAnnotation=None
    for item in iface.mapCanvas().items():
        if isinstance( item, QgsTextAnnotationItem ) and item.isSelected():
            selectedAnnotation = item
            break
    if selectedAnnotation:
        selectedAnnotation.setZValue(1) # See notes below
        event = QMouseEvent( QEvent.MouseButtonDblClick, 
                             selectedAnnotation.pos().toPoint(), 
                             Qt.LeftButton, 
                             Qt.LeftButton, 
                             Qt.NoModifier )
        QApplication.sendEvent( iface.mapCanvas().viewport(), event ) 
        selectedAnnotation.setZValue(0)

This function needs the Text Annotation tool activated (currently selected) to work. You can ensure that programmatically inside your plugin, have a look at How can I select the Text Annotation tool through python.

You can test the function by running it directly into the QGIS Python Console, selecting a text annotation in the canvas, and then calling the fuction in this way: editSelectedAnnotation(). It should open the corresponding dialog.


Notes:

  • Annotations might be hidden by other annotations, so a single double click could fail. We need to raise selected annotation to the top (setting its Z value to 1, as all z values seem to be 0 by default), simulate the double click, and then reset the Z value to 0.
  • This answer is just a workaround to solve your specific problem. The ideal case would be to able to open the dialog from the QGIS API itself.
  • I suggest you to raise QGIS developers awareness of use cases like yours, as well as to open a ticket explaining why it would be a good idea to expose QGIS Text Annotation dialog (or maybe just the Annotation Tool) through the QGIS API. Nowadays, devs are making changes to the API, so they might find this issue interesting.
1
  • 1
    Thanks for the descriptive answer, it helped me a lot. I'll try to open a ticked as you suggested with such case. Your help is really appreciated.
    – Mai
    Commented Dec 25, 2016 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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