Reputation: 148
I have developed a gui in Qt Designer, that among other things, displays a QTextEdit widget.
I need to handle the user entering the certain key combinations (TAB key, SHIFT + ENTER and SHIFT + RETURN so far) in such a way that it doesn't display those characters in the QTextEdit widget and implements some custom functionality.
I have created a handler to detect keyReleaseEvents, but that seems to be too late to prevent tabs, carriage returns etc. getting displayed, so then I tried handling keyPressEvents, but that doesn't seem to work at all. From what I've read online, people are saying that the keyPressEvent is getting consumed by the widget and so never gets propagated up to the gui. I don't know enough about QTextEdit widgets to say if that's correct, but for now I'm assuming it is.
So then I tried this in the class __init__
method for the gui (I have left out the check for the RETURN key to keep the code short below:
self.textEdit.event = self.eventX
Here's the code for the eventX method:
def eventX(self, event):
if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
print 'CODE WINDOW: Tab pressed'
self.textEdit.insertPlainText(QString('| |'))
self.textEdit.update()
event.accept()
return True
elif event.type() == QEvent.KeyPress and (event.key() == Qt.Key_Enter) and event.modifiers() & Qt.ShiftModifier:
print 'CODE WINDOW: Shift pressed with Enter'
self.submitCode()
self.textEdit.update()
event.accept()
return True
else:
event.ignore()
return QWidget.event(self, event)
This is where things get strange. The code above will sort of work if I have specified that:
verticalScrollBarPolicy = Qt.ScrollBarAsNeeded
for the QTextEdit widget, except that it won't actually display the characters in the widget, but it does seem to handle the TAB and SHIFT-ENTER/SHIFT-RETURN keys being pressed?
And then, if I set verticalScrollBarPolicy = Qt.ScrollBarAlwaysOn
, the eventX method seems to get completely ignored!
This is driving me crazy at this stage. Does anyone know what I'm doing wrong?
Thanks.
UPDATE: Based on the information from MDURANT, I now have the code below that works for me:
def handleEditorKeyPress(self, event):
if event.key() == Qt.Key_Tab:
# My custom code goes here.
event.accept()
return
elif event.key() == Qt.Key_Enter and event.modifiers() & Qt.ShiftModifier:
# My custom code goes here.
event.accept()
return
elif event.key() == Qt.Key_Return and event.modifiers() & Qt.ShiftModifier:
# My custom code goes here.
event.accept()
return
else:
event.ignore()
return QTextEdit.keyPressEvent(self.textEdit, event)
Upvotes: 0
Views: 2166
Reputation: 28684
An example of capturing key events. In this case, I simply pass the event through to another identical widget, but in general, you can set the event function to anything
class win(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self, parent=None)
self.box = QtGui.QHBoxLayout(self)
self.setLayout(self.box)
self.ed1 = QtGui.QTextEdit(self)
self.ed2 = QtGui.QTextEdit(self)
self.ed1.keyPressEvent = self.ed2.keyPressEvent
self.ed1.keyReleaseEvent = self.ed2.keyReleaseEvent
self.box.addWidget(self.ed1)
self.box.addWidget(self.ed2)
See the documentation of the event: http://qt-project.org/doc/qt-4.8/qtextedit.html#keyPressEvent
Upvotes: 1