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?

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
  • 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 '19 at 10:17
  • 1
    Ooops!I had not seen your comment @joseph,sorry – Fran Raga Apr 15 '19 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 '19 at 10:36

1 Answers1

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

Fran Raga
  • 7,838
  • 3
  • 26
  • 47