Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am no programmer so I do not understand how to create a plugin to add a button to the toolbar. Is there another simpler way to do so?

I only need a few tools from the ftools suite and do not want the whole list to confuse my students, therefore I'm looking for a solution to add only individual buttons to the toolbars.

share|improve this question
    
Thanks for the answers and script. I have an extension to the question. How can I edit the script such that I can also add icons from other menus? E.g. Heatmap from Raster and Identify Features. My whole idea is to add the most essential tools in a single tool bar. –  Michael Soo Oct 4 '12 at 1:33

1 Answer 1

There is no simple way to do what you want using current application preferences, but it can readily be done with a background plugin that loads at launch.

Plugin Directory Structure

CustomToolbar
  __init__.py
  customtoolbar.py
  icon.png
  metadata.txt

Contents of Files

customtoolbar.py - this is the main plugin Python class that creates the custom toolbar from existing menu actions. You'll want to edit the list values for acttxts to reflect the actions from the Vector menu that you want on the toolbar.

# -*- coding: utf-8 -*-
"""
/***************************************************************************
 CustomToolbar
                                 A QGIS plugin
 Template plugin for creating a custom toolbar from existing menu actions.
                             -------------------
        begin                : 2012-09-26
        copyright            : (C) 2012 by Me
        email                : email@example.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 This script initializes the plugin, making it known to QGIS.
"""

class CustomToolbar:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        self.toolbar = None

    def initGui(self):
        # Don't add the custom toolbar unless all other plugins are loaded
        if self.iface.mainWindow().isVisible():
            self.addCustomToolbar()
        else:
            # Qt signal/slot connection fired after app is fully launched
            self.iface.initializationCompleted.connect(self.addCustomToolbar)

    def unload(self):
        # Remove toolbar on plugin unload
        if self.toolbar is not None:
            self.iface.mainWindow().removeToolBar(self.toolbar)

    def addCustomToolbar(self):
        # Found actions to add to custom toolbar
        addacts = []

        # Text of actions to add from Geoprocessing Tools submenu
        acttxts = ["Intersect", "Buffer(s)", "Union"]

        vm = self.iface.vectorMenu()
        for vmact in vm.actions():  # most 1st level submenu actions have menus
            if not vmact.menu():  # skip 1st level actions with no menu
                continue
            for act in vmact.menu().actions():  # 2nd level submenu actions
                if act.text() in acttxts:
                    addacts.append(act)

        if addacts:
            self.toolbar = self.iface.addToolBar("Custom Toolbar")
            for act in addacts:
                self.toolbar.addAction(act)

__init__.py - necessary for loading the plugin package into QGIS, contains older (pre-QGIS 1.8) metadata.

# -*- coding: utf-8 -*-
"""
/***************************************************************************
 CustomToolbar
                                 A QGIS plugin
 Template plugin for creating a custom toolbar from existing menu actions.
                             -------------------
        begin                : 2012-09-26
        copyright            : (C) 2012 by Me
        email                : email@example.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 This script initializes the plugin, making it known to QGIS.
"""
def name():
    return "Custom Toolbar"
def description():
    return "Template plugin for creating a custom toolbar from existing menu actions."
def version():
    return "Version 0.1"
def icon():
    return "icon.png"
def qgisMinimumVersion():
    return "1.0"
def classFactory(iface):
    # load CustomToolbar class from file CustomToolbar
    from customtoolbar import CustomToolbar
    return CustomToolbar(iface)

metadata.txt - QGIS 1.8+ metadata for the plugin

# This file contains metadata for your plugin. Beginning
# with version 1.8 this is the preferred way to supply information about a
# plugin. The current method of embedding metadata in __init__.py will
# be supported until version 2.0

# This file should be included when you package your plugin.

# Mandatory items:


[general]
name=Custom Toolbar
qgisMinimumVersion=1.0
description=Template plugin for creating a custom toolbar from existing menu actions.
version=0.1

# end of mandatory metadata

# Optional items:

# Uncomment the following line and add your changelog entries:
# changelog=

# tags are comma separated with spaces allowed
tags=

homepage=
tracker=
repository=
icon=icon.png
# experimental flag
experimental=False

# deprecated flag (applies to the whole plugin, not just a single version
deprecated=False

# Author contact information
author=Me
email=email@example.com

icon.png - plugin's icon when viewing in Plugin Manager. The plugin will not be visible in the GUI, other than in the Manager and, of course, with its toolbar. For this icon, you can download the raw version of the default plugin icon from the QGIS source repository and rename it to icon.png.

Installation

Once you have the plugin directory created, you can move it to your users' QGIS plugins directory:

Linux and other unices:
/home/$USERNAME/.qgis/python/plugins
Mac OS X:
/Users/$USERNAME/.qgis/python/plugins
Windows:
C:\Documents and Settings\$USERNAME\.qgis\python\plugins

Once QGIS is launched, make sure the plugin is turned on in the Plugin Manager.

Note: the framework for this plugin was generated using the Plugin Builder plugin, then stripped of unnecessary files and code to create a slimmed-down background plugin for local use.

share|improve this answer
    
Your explanation is excellent but I have no clue how to go about it. You know anyone (or yourself) can help me do this for a small fee? Thanks. –  Michael Soo Sep 27 '12 at 1:32
    
Ok, I had a go at it after reading up on how to do it. After starting qGIS, I have this error message. Now it is really beyond me to understand where did I make any mistake. –  Michael Soo Sep 27 '12 at 5:37
    
Couldn't load plugin CustomToolbar due an error when calling its classFactory() method Traceback (most recent call last): File "D:/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py", line 164, in startPlugin plugins[packageName] = package.classFactory(iface) File "C:/Users/Michael/.qgis//python/plugins\CustomToolbar_init_.py", line 35, in classFactory from customtoolbar import CustomToolbar –  Michael Soo Sep 27 '12 at 5:40
    
File "D:/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py", line 309, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/Users/Michael/.qgis//python/plugins\CustomToolbar\customtoolbar.py", line 1 A QGIS plugin ^ IndentationError: unexpected indent –  Michael Soo Sep 27 '12 at 5:40
    
full error message.link.Can anyone enlighten? –  Michael Soo Sep 27 '12 at 8:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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