0

when the QTextEdit doesn’t have focus ,how to redirect keyboard events to the text editor ?

my code is here http://qt-project.org/forums/viewthread/30245/

thanks for your help ! I did want to intercept any key event at application level, I have also been told to use event filter at such case before ,but I have difficulty in implementing the eventFilter() function here ,can you show some code ?thanks in advance !

iMath
  • 2,326
  • 2
  • 43
  • 75
  • Does it not work exactly the same way as the wheel events in this question: http://stackoverflow.com/questions/17738997/how-to-redirect-wheel-events-of-qwidget-to-qtextedit –  Jul 20 '13 at 16:36
  • Perhaps the same ,but I don't understand his code .can you show me some code ? – iMath Jul 21 '13 at 12:50

2 Answers2

0

Use an event filter to redirect it from another part of your application.

Catching Qt modifier key releases

If your application is not focused at all, you would need to use a keyboard hook or something similar.

Hope that helps.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
0

This is how eventFilter could be implemented into your code:

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

class BoxLayout(QWidget):
    def __init__(self, parent=None):
        super(BoxLayout, self).__init__(parent)
#        self.resize(100, 300)

        ok = QPushButton("OK")
        cancel = QPushButton("Cancel")
        self.textEdit = QTextEdit()
        vbox = QVBoxLayout()
        vbox.addWidget(self.textEdit)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)
        self.setLayout(vbox)

        self.installEventFilter(self)





    def eventFilter(self, object_, event):
        if event.type() == QEvent.KeyRelease and self.focusWidget() != self.textEdit:
            self.textEdit.append(event.text())

        return False


app = QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())

If you want to prevent other widgets from receiving the keys just return True in the event Filter

BoshWash
  • 5,320
  • 4
  • 30
  • 50