7

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?

3
  • 2
    Where did you place the startup.py file? It should be placed in C:\Users\username\AppData\Roaming\QGIS\QGIS3 as described in this post. Your code should have produced an error about QAction not being defined which can be rectified by adding from PyQt5.QtWidgets import QAction.
    – Joseph
    Apr 15, 2019 at 10:17
  • 1
    Ooops!I had not seen your comment @joseph,sorry
    – Fran Raga
    Apr 15, 2019 at 10:34
  • 1
    @FranRaga - Don't be! I had a feeling the problem OP had was similar to mine so glad you answered this one too :)
    – Joseph
    Apr 15, 2019 at 10:36

1 Answer 1

9

Great idea

you need place startup.py in C:\Users\<username>\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

Your Answer

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

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