1

QGIS 3.0 is not backwardly compatible with QGIS 2.0, so currently my plugin (which was fine in the old version) is broken and I am trying to overhaul as required. The documentation seems to be taking a while to catch up with the software.

Would anyone be able to post some code which shows how to create a custom menu for a plugin in version 3.0?

My existing method (good in version 2.x / PyQt4) is this:

from PyQt4 import QtGui

self.menu = QMenu( "&Whatever", self.iface.mainWindow().menuBar() )
actions = self.iface.mainWindow().menuBar().actions()
lastAction = actions[-1]
self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )

mnuSub1 = self.menu.addMenu('Sub-menu')
mnuSub1.setIcon(QIcon(icoFolder + 'img1.png'))
mnuAction = mnuSub1.addAction("Do something...",self.RunSomething)
5

You can do exactly the same with PyQt5 [1] if you apply some minor changes.

  • Import from PyQt5
  • Many things have been split from the QtGui module to the QtWidgets module. Among them QMenu (note the qmake QT += widgets entry).

from PyQt5.QtWidgets import QMenu

self.menu = QMenu("&Whatever", self.iface.mainWindow().menuBar())
actions = self.iface.mainWindow().menuBar().actions()
lastAction = actions[-1]
self.iface.mainWindow().menuBar().insertMenu(lastAction, self.menu)

[1] Sidenote, the QGIS API itself is not actually relevant here, it's the PyQt docs which count. So I wouldn't blame the QGIS docs for not catching up.

  • Thanks. Problem solved. (My point about documentation was, I think, based on a misconception. I was looking for help in the PyQGIS Developer Cookbook and thought that there was nothing for Version 3.0, but I see that there is. It is just not named as such. It is called 'Documentation QGIS Testing'.) – wotnot Apr 28 '18 at 14:53
0

In QGIS 3, QgisInterface provices

  • addPluginToDatabaseMenu Add action to the Database menu
  • addPluginToMenu Add action to the plugins menu
  • addPluginToRasterMenu Add action to the Raster menu
  • addPluginToVectorMenu Add action to the Vector menu
  • addPluginToWebMenu Add action to the Web menu

As documented in http://python.qgis.org/master/gui/other/QgisInterface.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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