简体   繁体   English

qtreeview的qt eventfilter不起作用?

[英]qt eventfilter for qtreeview not working?

I wish for an "event" to happen when enter key is pressed while a qtreeview is selected. 我希望在选择qtreeview时按Enter键时发生“事件”。 As I prefer not to subclass qtreeview (easier for the designer) - I tried to install an event filter. 由于我不希望将qtreeview子类化(对于设计人员来说更容易)-我尝试安装事件过滤器。 However this didn't seem to work: 但是,这似乎不起作用:

The class simply contains a public function: 该类仅包含一个公共函数:

bool InputTreeEventHandler::eventFilter(QObject *obj, QEvent *event) const {
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyevent = dynamic_cast<QKeyEvent*>(event);
        QTreeView* tree = dynamic_cast<QTreeView*>(obj);
        if (keyevent->key() == Qt::Key_Enter) {
            //code
        }
    } else {
        return false;
    }

}

And the event is added like the following: 并添加了如下事件:

ui.InputTreeView->installEventFilter(InputTreeKeyboardEater.get());

Where ui.InputTreeView is the treeview I wish to act when pressing enter, and InputTreeKeyboardEater a (shared) pointer to an object of InputTreeEventHandler 其中ui.InputTreeView是我希望按Enter键时要操作的树视图,而InputTreeKeyboardEater是指向InputTreeEventHandler对象的(共享)指针

When putting a breakpoint at start of function above it shows the whole event handler isn't even called - what can I be doing wrong? 当在函数上方的位置放置一个断点时,它表明甚至没有调用整个事件处理程序-我该怎么做?

See here -- QObject::eventFilter isn't const, which would explain your problem. 参见此处 QObject::eventFilter不是const,这可以解释您的问题。 InputTreeView is looking to call a non-const version, which isn't there. InputTreeView希望调用不存在的非const版本。 Also eventFilter is protected not public though I don't think that's critical. 另外,尽管我认为这不是至关重要的,但eventFilter也可以不受公开保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM