Why isn't my Qt eventFilter picking up mouse events on a QTreeWidget?

2k Views Asked by At

I have basically this code to intercept certain QTreeWidget events.

MyWidget :: MyWidget ()
{
     m_tree = new QTreeWidget ();
     // ...
     m_tree -> installEventFilter (this);
}

bool MyWidget :: eventFilter (QObject * obj, QEvent * e)
{
    if (m_tree != obj)
        return QWidget :: eventFilter (obj, e);

    qDebug () << e -> type ();

    switch (e -> type ())
    {
        case QEvent :: MouseButtonPress:
        case QEvent :: MouseButtonRelease:
        case QEvent :: MouseMove:
        case QEvent :: Leave:
            qDebug () << "GOT EM";
            break;
    };

    // ...
 }

As I click and move around in the QTreeWidget, the event handler prints out e->type() for lots of events, but not the mouse events. Mouse events appear never to happen (with the exception of Leave).

Why is this happening? I should be getting move events even with mouse tracking off provided at least one button is down, and I should be getting press and release events regardless. The QTreeWidget itself behaves normally, as if no event handler is installed.

1

There are 1 best solutions below

0
László Papp On

Mouse events seem to be handled by the viewport on the tree view, so you need to install your event filter on the viewport of the tree view, not the tree view itself.

MyWidget :: MyWidget ()
{
     m_tree = new QTreeWidget ();
     // ...
     m_tree -> viewport() -> installEventFilter (this);
}

bool MyWidget :: eventFilter (QObject * obj, QEvent * e)
{
    if (m_tree -> viewport() != obj)
        return QWidget :: eventFilter (obj, e);

    qDebug () << e -> type ();

    switch (e -> type ())
    {
        case QEvent :: MouseButtonPress:
        case QEvent :: MouseButtonRelease:
        case QEvent :: MouseMove:
        case QEvent :: Leave:
            qDebug () << "GOT EM";
            break;
    };

    // ...
 }