[GIS] In QGIS is there a keyboard shortcut to open/close the layers panel

qgis

Is there a keyboard shortcut, or even something like a toolbar button, which would make it easier and quicker to open (and close) the layers panel in QGIS (2.6).

I don't see one listed under the keyboard shortcuts list.

Best Answer

It's not built in (yet. I might just put it on my todo xmas list) however you can use some Python to do this:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

dock = iface.mainWindow().findChild(QDockWidget, "Layers")

def activated():
    visible = dock.isVisible()
    dock.setVisible(not visible)

short = QShortcut(QKeySequence(Qt.ALT + Qt.Key_1), iface.mainWindow())
short.setContext(Qt.ApplicationShortcut)
short.activated.connect(activated)

To have this always on you can add it to .qgis2\python\setup.py and it will be run each time QGIS starts.

Here is some code you can add to your startup.py in order to control bindings

from functools import partial
from qgis.utils import iface

from PyQt4.QtCore import *
from PyQt4.QtGui import *

mappings = {"Layers": Qt.ALT + Qt.Key_1,
            "Browser": Qt.ALT + Qt.Key_2,
            "PythonConsole": Qt.ALT + Qt.Key_3}
shortcuts = []

def activated(dock):
    dock = iface.mainWindow().findChild(QDockWidget, dock)
    visible = dock.isVisible()
    dock.setVisible(not visible)

def bind():
    for dock, keys in mappings.iteritems():
        short = QShortcut(QKeySequence(keys), iface.mainWindow())
        short.setContext(Qt.ApplicationShortcut)
        short.activated.connect(partial(activated, dock))
        shortcuts.append(short)

bind()

This will take all the mappings and create shortcuts for them. Nifty!

Related Question