5

I have been using the python solution presented in this answer, which has worked well from 2.6 to 2.10 for me, and still works in the LTR 2.8.4. I can't get it to work with 2.12 (and recent point release 2.12.1).

I have my .qgis2/python/startup.py configured based on the answer linked above.

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}
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()

From the python console I am able to toggle the visibility of each panel using .setVisible(True/False). However I can't seem to get the keys to bind.

5

EDIT: This works on QGIS 2.12.1 from osgeo4w (Windows 8.1), but does not work on QGIS 2.12.1 from macports (OSX 10.11).

It appears that QT.Alt no longer works in QGIS 2.12. The correct reference is QT.Key_Alt. Enlightened by the link to the QT Key reference in this answer.

The code below works with QGIS 2.12.1

from functools import partial
from qgis.utils import iface

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

mappings = {"Layers": Qt.Key_Alt + Qt.Key_1,
            "Browser": Qt.Key_Alt + Qt.Key_2}
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()

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.