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.