29

I am building a UI with Qt Creator and I want buttons to perform different actions with different modifiers. So I thought I could call functions with dynamic string properties that would perform the action depending on the modifier.

Is there a simpler way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1087058
  • 349
  • 1
  • 3
  • 5
  • 2
    See http://stackoverflow.com/questions/3100090/howto-detect-the-modifier-key-on-mouse-click-in-qt – Tanriol Jan 07 '12 at 19:58
  • Do you mean that you want to use `MouseClick+Modifier` to run commands? And what does "dynamic string properties" mean? – ekhumoro Jan 08 '12 at 01:34
  • Hi, I meant when someone shift clicks a button in a qt ui it performs a different action than ctrl click or just regular click. – user1087058 Jan 08 '12 at 02:12

3 Answers3

58

It looks like all you need to do is check the keyboardModifiers in your button handler, and select a different action as appropriate. The various modifiers can be OR'd together in order to check for multi-key combinations:

PyQt5:

import sys
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.button = QtWidgets.QPushButton('Test')
        self.button.clicked.connect(self.handleButton)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        modifiers = QtWidgets.QApplication.keyboardModifiers()
        if modifiers == QtCore.Qt.ShiftModifier:
            print('Shift+Click')
        elif modifiers == QtCore.Qt.ControlModifier:
            print('Control+Click')
        elif modifiers == (QtCore.Qt.ControlModifier |
                           QtCore.Qt.ShiftModifier):
            print('Control+Shift+Click')
        else:
            print('Click')

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())

PyQt4:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button = QtGui.QPushButton('Test')
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        modifiers = QtGui.QApplication.keyboardModifiers()
        if modifiers == QtCore.Qt.ShiftModifier:
            print('Shift+Click')
        elif modifiers == QtCore.Qt.ControlModifier:
            print('Control+Click')
        elif modifiers == (QtCore.Qt.ControlModifier |
                           QtCore.Qt.ShiftModifier):
            print('Control+Shift+Click')
        else:
            print('Click')

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
7

I was trying to handle multiple keys pressed at the same time (e.g. A and W or W and D). The solution below works with multiple keys being pressed at the same time (including Ctrl, Shift, Alt, etc.).

def keyPressEvent(self, event):
    self.firstrelease = True
    astr = "pressed: " + str(event.key())
    self.keylist.append(astr)

def keyReleaseEvent(self, event):
    if self.firstrelease == True:
        self.processmultikeys(self.keylist)

    self.firstrelease = False
    del self.keylist[-1]

def processmultikeys(self, keyspressed):
    # Your logic here
    print keyspressed

Go here for the original discussion of this solution: How to get multiple key presses in single event?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul
  • 2,409
  • 2
  • 26
  • 29
3

Here's another approach using bit operators, that avoids getting into many combinations.

@classmethod
def get_key_modifiers(cls):
    QModifiers = Qt.QtWidgets.QApplication.keyboardModifiers()
    modifiers = []
    if (QModifiers & Qt.QtCore.Qt.ShiftModifier) == Qt.QtCore.Qt.ShiftModifier:
        modifiers.append('shift')
    if (QModifiers & Qt.QtCore.Qt.ControlModifier) == Qt.QtCore.Qt.ControlModifier:
        modifiers.append('control')
    if (QModifiers & Qt.QtCore.Qt.AltModifier) == Qt.QtCore.Qt.AltModifier:
        modifiers.append('alt')
    return modifiers
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric
  • 31
  • 1