【问题标题】:PyQt4 - Enter key pressed on item in QListViewPyQt4 - 在 QListView 中的项目上按下 Enter 键
【发布时间】:2011-02-22 14:32:48
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: python qt4 pyqt4


    【解决方案1】:

    使用事件过滤:例如在列表容器的 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 键的逻辑事件。您可以使用其他方法来捕捉事件,但我所展示的内容应该会为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多