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 ?
as This saves the user from clicking the text editor before entering text, making the application more user-friendly.some people told me to use Event Filters ,I did use 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_())@
-
Firstly, I would not recommend that you actually do this in an application. What you see as making the app more 'user friendly' actually breaks with the key concepts of user expectation and OS native behaviours that Qt promotes. That said (and my conscience appeased ;o) and seeing as you asked me directly (via a PM) here is a simple solution:
@
from PyQt4.QtGui import *
from PyQt4.QtCore import *class BoxLayout(QWidget):
def init(self, parent=None):
QWidget.init(self, parent)self.resize(100, 300) vbox=QVBoxLayout(self) self.textEdit = QTextEdit(""" 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.addWidget(self.textEdit) vbox.addWidget(QPushButton("OK", self)) vbox.addWidget(QPushButton("Cancel", self)) self.setFocus() def keyPressEvent(self, event): self.textEdit.setFocus() self.textEdit.keyPressEvent(event)
if name=="main":
from sys import argv, exitapp=QApplication(argv) qb=BoxLayout() qb.show() qb.raise_() exit(app.exec_())
@
The keyPressEvent() method is the important bit here: when the main widget receives a key press event focus is moved to the QTextEdit and the initial event is forwarded.
Hope this answers your question ;o)
-
thanks , jazzycamel,you're a very kind-hearted person .
you said
[quote author="jazzycamel" date="1374860919"] What you see as making the app more 'user friendly' actually breaks with the key concepts of user expectation and OS native behaviours that Qt promotes.
[/quote]I am curious how you learned of this ?
for this question ,I found another solution ,code here
@
def changeEvent(self, event):
if event.type()==QEvent.ActivationChange:
self.textEdit.setFocus()
@as for you solution , I also thought of that, but there is some problem with it when you type words into the textEdit using a input software ,for example ,I use "Sogou Pinyin":http://en.wikipedia.org/wiki/Sogou_Pinyin
,I don’t know why the problem occurred …
Thanks anyway.