PyQt4 - 在 QListView 中的项目上按下 Enter 键

Joh*_*nes 1 python qt4 pyqt4

嘿。我有一个 QListView,到目前为止我只知道如何使用已经给出的信号。当在列表中的项目 (QStandardListItem) 上按下 Enter 键时,我找不到任何信号。似乎也找不到任何 keyPressedEvents。

是否可以像这样将 QListView 与事件“挂钩”?如何?:)

谢谢

Vin*_*jip 5

使用事件过滤:例如在列表容器的 setupUi 中,执行

# the self param passed to installEventFilter indicates the object which
# defines eventFilter(), see below:
self.list.installEventFilter(self)
Run Code Online (Sandbox Code Playgroud)

然后在该容器中定义过滤器 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
Run Code Online (Sandbox Code Playgroud)

请注意,这InsertParagraphSeparator是绑定到 Enter 键的逻辑事件。您可以使用其他方法来捕捉事件,但我所展示的应该为您指明正确的方向。