【发布时间】:2011-02-22 14:32:48
【问题描述】:
嘿。 我有一个 QListView,到目前为止,我只知道如何使用已经给出的信号。当在列表中的项目(QStandardListItem)上按下回车键时,我找不到任何信号。似乎也找不到任何 keyPressedEvents。
是否可以像这样将 QListView 与事件“挂钩”?如何? :)
谢谢
【问题讨论】:
嘿。 我有一个 QListView,到目前为止,我只知道如何使用已经给出的信号。当在列表中的项目(QStandardListItem)上按下回车键时,我找不到任何信号。似乎也找不到任何 keyPressedEvents。
是否可以像这样将 QListView 与事件“挂钩”?如何? :)
谢谢
【问题讨论】:
使用事件过滤:例如在列表容器的 setupUi 中,做
# the self param passed to installEventFilter indicates the object which
# defines eventFilter(), see below:
self.list.installEventFilter(self)
然后在该容器中定义过滤器 API 函数:
def eventFilter(self, watched, event):
if event.type() == QEvent.KeyPress and \
event.matches(QKeySequence.InsertParagraphSeparator):
i = self.list.currentRow()
# process enter key on row i
请注意,InsertParagraphSeparator 是绑定到 Enter 键的逻辑事件。您可以使用其他方法来捕捉事件,但我所展示的内容应该会为您指明正确的方向。
【讨论】: