7

Is it possible to hide/remove/customize the default UI of QGIS from a Python plugin?

I want to add my own menus and toolbars and remove some included by default in QGIS.

If it's possible, which reference should I use?

8

Yes, it's possible to customize the QGIS UI from a Python plugin by both adding your own toolbars and menus as well as removing/hiding QGIS toolbars and menus.

These would be the code snippets for each situation:

  1. Adding a toolbar:

    # Add a custom toolbar
    self.toolbar = self.iface.addToolBar( "My tools" )
    self.toolbar.setObjectName( "My tools" )
    self.toolbar.addAction( self.action )
    
  2. Removing a QGIS toolbar:

    # Remove a QGIS toolbar (e.g., the File toolbar)
    fileToolBar = self.iface.fileToolBar()
    self.iface.mainWindow().removeToolBar( fileToolBar )
    
  3. Adding a menu:

    # Add a custom menu
    self.menu = QMenu( "&My tools", self.iface.mainWindow().menuBar() )
    self.menu.setObjectName( "&My tools" )
    actions = self.iface.mainWindow().menuBar().actions()
    lastAction = actions[-1]
    self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )
    self.menu.addAction( self.action )
    
  4. Removing a QGIS menu:

    # Remove a QGIS menu (e.g., the Edit menu)
    editMenu = self.iface.editMenu()
    editMenu.menuAction().setVisible( False )
    

You can append such code to the initGui() method of your plugin. I assumed you have an action created in such method, as any plugin has.

You can see these code snippets implemented in a test plugin that I've created and published here. In the README file you find instructions for both installing and using it.


Note: You can get a reference of QGIS menus and toolbars from Python by using methods exposed by iface. A list of such methods can be found in the QGIS docs.

  • nice code snippets.just one note to code snippet 3:If you reload the plugin(with plugin reloader for example) you will always get one more of your custom menus. but i fixed it with the help of one other answer of you: gis.stackexchange.com/questions/169869/… – Thomas B Feb 25 '16 at 19:50
2

open .ui file with QT Designer or QT Creator and follow QT guides. Other QGIS oriented references are: PyQGIS Programmers's Guide - https://locatepress.com/ppg and some more Packt books in publishing

  • 1
    I wanna do it using python plugin. don't wanna modify qgis ui file – ashish Dec 19 '14 at 9:26
  • latest version of plugin builder use onthefly compiling of .ui file so you don't have is compiled in as .py. Just go in <your plugin>_dialog.py and remove FORM_CLASS, _ = uic.loadUiType... with import to your .ui compilation .ui compilation can be done manually with pyuic4 -o outfile.py <your ui file> – Luigi Pirelli Dec 19 '14 at 11:36
  • after compilation you'll have a .py that you can modify manually using pyqt as usual – Luigi Pirelli Dec 19 '14 at 11:39

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.