I wish for an “event” to happen when enter key is pressed while a qtreeview is selected. As I prefer not to subclass qtreeview (easier for the designer) – I tried to install an event filter. 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
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::eventFilterisn’t const, which would explain your problem.InputTreeViewis looking to call a non-const version, which isn’t there. AlsoeventFilteris protected not public though I don’t think that’s critical.