user595985
user595985

Reputation: 1583

Detecting Key Sequences

I am using pyqt's Qshortcut in trying to detect a key combination to trigger some action i.e when the user types -> into a QtextEdit widget, I would like to print "changing mode". I have seen other key sequence examples that involve CTRL+E or some other CTRL or shift key combination,

    self.shcut1 = QtGui.QShortcut(self)
    self.shcut1.setKey("CTRL+E")
    self.connect(self.shcut1, QtCore.SIGNAL("activated()"), self.close)

But I really want to trap ->(hyphen followed by a greater than sign). Any suggestions on how to do this

Upvotes: 2

Views: 2661

Answers (2)

user1006989
user1006989

Reputation:

QShortCut only accepts combinations of QtCore.Qt::KeyboardModifiers. Try using an event filter:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QTextEdit):
    modeChanged = QtCore.pyqtSignal(bool)
    _seenMinus  = False
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.installEventFilter(self)

        self.modeChanged.connect(self.on_modeChanged)

    def on_modeChanged(self):
        print "Changing Mode."

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.KeyPress:
            if event.key() == QtCore.Qt.Key_Minus:
                self._seenMinus = True

            elif event.key() == QtCore.Qt.Key_Greater \
            and  event.modifiers() == QtCore.Qt.ShiftModifier \
            and  self._seenMinus:
                self.modeChanged.emit(True)
                self.setStyleSheet("""
                    background-color: lightgray;
                """)

            elif event.modifiers() != QtCore.Qt.ShiftModifier:
                if self._seenMinus == True:
                    self.modeChanged.emit(False)
                    self._seenMinus = False
                    self.setStyleSheet("")


        return super(MyWindow, self).eventFilter(obj, event)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())

Upvotes: 1

Matt Phillips
Matt Phillips

Reputation: 9691

Just catch the signal QTextEdit::textChanged(), and every the user makes a change, scan the text for '->'. Granter the brute force approach of scanning the entire text block every time isn't pretty; another option is scanning only the last two characters of the text. However this misses the case where the user creates '->' by deleting text in between a '-' and a '>' character. If you're not worried about that case, then just go with the last two. QTextEdit::cursorPositionChanged might allow you to test precisely at the insertion/deletion point.

Upvotes: 1

Related Questions