【问题标题】:How Do I Disable the Select All QTreeView Key Binding如何禁用全选 QTreeView 键绑定
【发布时间】:2012-07-30 10:12:54
【问题描述】:

我在 Maya(一个 3D 计算机图形应用程序)的一个小部件中有 QTreeWidget。问题是,我的小部件不仅阻止了本机 CTRL+A 热键,它还在选择我的树中的所有内容。如何让这个热键冒泡到父应用程序?

我已经在使用事件过滤器来处理很多自定义键,但树似乎想为我处理这个。我喜欢箭头键功能,所以我不想禁用所有键绑定,但如果必须,我会......如果我知道怎么做

干杯,

附:这里提出了类似的问题,但答案忽略了这个问题: qt: I would like to disable the key bindings automatically set for a QTreeView


这里是 A 在 Python 中用于 QTreeWidget 的解决方案。

我无法确定这是我用 Python 做过的最肮脏的事情,还是只是一个漂亮的 Python 版本的扩展方法! (感觉是前者。)

问题是,我实际上没有 QTreeView 类。它是在设计器中添加的,名称为“tree”,所以我从字面上覆盖了该方法,然后从 my 方法调用基本功能......

def __init__ #...snip...
    self.tree.keyPressEvent = self.onKeyPressEvent # Replace with my method


def onKeyPressEvent(self, event):
    if event.key() == Qt.Key_A and event.modifiers() == Qt.ControlModifier:
        event.ignore()  # Allows fall-through to the parent
        return

    QtGui.QTreeView.keyPressEvent(self.tree, event)  # All other behaviors handled

...所以这对我来说太脏了。我知道必须有解决这种常见情况的方法(没有子类)。我已经在使用事件过滤器,所以我尝试了它并且它有效。关键是知道事件在哪里被处理并使用event.ignore() 以及返回True 以允许事件冒泡并阻止KeyPressEvent 用于CTRL+A。

def eventFilter(self, obj, event):
    # Filter out all non-KeyPress events
    if not event.type() == QEvent.KeyPress:
        return False

    if event.key() == Qt.Key_A and event.modifiers() == Qt.ControlModifier:
        event.ignore()  # Allows fall-through to the parent
        return True     # Block the tree's KeyPressEvent

    return False        # Do nothing

【问题讨论】:

    标签: qt key-bindings maya qtreeview qtreewidget


    【解决方案1】:

    重新实现keyPressEvent,当按下CTRL+A时,忽略该事件。

    然后代码应该如下所示:

    MyTreeView::keyPressEvent(QKeyEvent *e)
    {
        if(e->key() == Qt::Key_A && e->modifiers() == Qt::ControlModifier)
        {
            e->ignore();
            QWidget::keyPressEvent(e); // Not sure about this. Please try and report!
        }
        else
            QTreeView::keyPressEvent(e);
    }
    

    【讨论】:

    • 奇怪。当按下键或按下修饰符时,我会得到单独的事件,但是当收到“a”时,修饰符始终为假。我将在我的问题中添加一个示例
    • 完美!不需要 QWidget.keyPressEvent。忽略就行了!如果没有这个事件,请查看我上面的问题以获得结果。
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多