Thursday, 27 October 2016

pyqgis 3 - Add Help Menu entry in QGIS 3 from `startup.py`



I would like to add a menu entry in the Help menu pointing to some web ressource, say https://gis.stackexchange.com. The following code executed from the python console works perfect:


from qgis.utils import iface
import webbrowser

def open_gis_se():
webbrowser.open('https://gis.stackexchange.com')

iface.helpMenu().addSeparator()

gis_se_action = QAction('Go to gis.stackexchange')

iface.helpMenu().addAction(gis_se_action)
gis_se_action.triggered.connect(open_gis_se)


Result when typed in the python console:


enter image description here


... but putting it into my startup.py has no effect (Help menu remains 'as it is').


In QGIS 2, the above code put in the startup.py adds the desired menu entry as expected.


Why?



Answer




Great idea


you need place startup.py in C:\Users\\AppData\Roaming\QGIS\QGIS3 and add missing import ,and voilá


from qgis.utils import iface
from PyQt5.QtWidgets import QAction
import webbrowser

def open_gis_se():
webbrowser.open('https://gis.stackexchange.com')

iface.helpMenu().addSeparator()


gis_se_action = QAction('Go to gis.stackexchange')
iface.helpMenu().addAction(gis_se_action)
gis_se_action.triggered.connect(open_gis_se)

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...