读书人

【PyQt】redirect keyboard events to

发布时间: 2013-11-04 16:56:03 作者: rapoo

【PyQt】redirect keyboard events to QTextEdit when it has no focus ?
本帖最后由 redstoneleo 于 2013-07-21 16:19:46 编辑 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.



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_())
PyQt Python qt qt4 pyside
[解决办法]
qt有sendEvent和postEvent可以给特定的widget发送event。sendEvent要求目标widget立即处理该event,而postEvent是把该event加到消息队列中。参考https://qt-project.org/doc/qt-4.8/eventsandfilters.html的最后部分。

读书人网 >perl python

热点推荐