Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

EventFilter on QTreeView, no Qt::Key_delete event



  • Hi

    I want to be able to react on the "Delete" key in the QTreeView. Therefore I install an eventfilter on the QTreeView instance in the constructor of my mainwindow, as suggested in the docs:

    @ArinnotechModule::ArinnotechModule(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ArinnotechModule)
    {
    ui->setupUi(this);
    ui->objectTreeView->installEventFilter(this);
    }@

    Declaration and implementation of the event-filter method:
    @
    class ArinnotechModule : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit ArinnotechModule(QWidget *parent = 0);
    ~ArinnotechModule();
    ...
    virtual bool eventFilter(QObject * target, QEvent * event);
    ...
    };
    @
    @bool ArinnotechModule::eventFilter(QObject *target, QEvent event)
    {
    bool key = false;
    bool delKey = false;
    if( event->type() == QEvent::KeyPress ){
    key = true;
    QKeyEvent * keyEvent = static_cast<QKeyEvent
    >(event);
    if( keyEvent->key() == Qt::Key_Delete ){
    delKey = true;
    return true;
    }
    }
    return QMainWindow::eventFilter( target, event );
    }@

    I do get several keypress-events, e.g. F5, Enter, Space, Home, End, ... but Delete never makes it to the filter. What could be the problem?

    What I've tried so far:

    Subclass QTreeView, overwrite keyPressEvent(...)-method. Same behaviour.

    Sublcass QTreeView, overwrite event(...)-method. Same behaviour.

    The QTreeView is located in:
    QMainWindow->QWidget (Central Widget)->QTabWidget->QWidget (page)->QTreeView

    Any ideas why the filter does not capture the event?



  • Well, turns out I had prepared a QAction in the UI-Designer with the shortcut set to the "Delete" key. I figure the QAction object got the event first and handled it returning "true". After removing the shortcut everything worked as expected...


Log in to reply