Redirect keyboard events to QTextEdit when it has no focus ?
-
when the QTextEdit doesn’t have focus ,how to redirect keyboard events to the text editor ?
@
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)
app = QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())@
-
Hi redstoneleo,
(explained using the C++ prototypes)
To receive keyboard events on your QTextEdit, you can- Give it the focus after creating it :
@QWidget::setFocus(Qt::OtherFocusReason)
@ - Redirect focus from one widget to another (lets say windows to your edit)
@QWidget::setFocusProcus(QWidget*)
myWindow->setFocusProxy(myTextEdit);
@ - Prevent other widgets from gaining focus using
@QWidget::setFocusPolicy(Qt::NoFocus)
@
Or if you need to intercept any key event at application level, you can create an QObject class: reimplement the method
@bool QObject::eventFilter(QObject*, QEvent*)
@And use QObject::installEventFilter(QObject*) with this class as parameter, on the window, or the application.
- Give it the focus after creating it :
-
[quote author="Adrien Leravat" date="1374482323"]Hi redstoneleo,
(explained using the C++ prototypes)
To receive keyboard events on your QTextEdit, you can- Give it the focus after creating it :
@QWidget::setFocus(Qt::OtherFocusReason)
@ - Redirect focus from one widget to another (lets say windows to your edit)
@QWidget::setFocusProcus(QWidget*)
myWindow->setFocusProxy(myTextEdit);
@ - Prevent other widgets from gaining focus using
@QWidget::setFocusPolicy(Qt::NoFocus)
@
Or if you need to intercept any key event at application level, you can create an QObject class: reimplement the method
@bool QObject::eventFilter(QObject*, QEvent*)
@And use QObject::installEventFilter(QObject*) with this class as parameter, on the window, or the application.[/quote]
thanks for your help !
yes ,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 ! - Give it the focus after creating it :
-
the following is my code of using Event Filters ,but it doesn't work , anyone can help ?
@
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 =QLineEdit("""This function returns true if the contents of the MIME data object, specified by source , can be decoded and inserted into the document. It is called for example when during a drag operation the mouse enters this widget and it is necessary to determine whether it is possible to accept the drag and drop operation.""") vbox = QVBoxLayout() vbox.addWidget(self.textEdit) vbox.addWidget(ok) vbox.addWidget(cancel) self.setLayout(vbox) self.textEdit.installEventFilter(self) def eventFilter(self, obj, event): if obj == self and event.type() == QKeyEvent: self.textEdit.setFocus () self.textEdit.keyPressEvent(event) return True else: return QMainWindow.eventFilter(self, obj, event)
app = QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())@
-
you need to install the eventfilter on the QApplication instance ;)
-
[quote author="raven-worx" date="1374845629"]you need to install the eventfilter on the QApplication instance ;)[/quote]
if I install the eventfilter on the QApplication instance ,so what should be modified in the eventfilter function ?
this question has stuck me for a few days , I wonder if you could help ,thanks in advance !!! -
Where did you do that? As far as I can tell you installed it on the textEdit.