4

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.

John_West
  • 2,239
  • 4
  • 24
  • 44
spraff
  • 32,570
  • 22
  • 121
  • 229
  • 5
    The events don't happen on the QTreeView, they happen on the viewport. Install the viewport on `QTreeView::viewport()` instead. Or, override `mousePressEvent`, `mouseReleaseEvent`, etc (as all these do not need an event filter for QTreeView). – Alex Huszagh Jan 03 '16 at 18:17
  • Sorry I should specify: the mouse click events do not occur on the QTreeView, they occur on the viewport. Most other events occur on the QTreeView. – Alex Huszagh Jan 03 '16 at 18:28
  • 2
    @AlexanderHuszagh to elaborate: even though the event happens on the viewport, `mousePressEvent` and friends will catch the event when overridden for `QTreeView` because the widget already has an event filter installed on the viewport internally (QAbstractScrollArea). – Johannes Schaub - litb Jan 03 '16 at 18:35
  • Thanks. Want to turn that into an answer? – spraff Jan 03 '16 at 20:55
  • Sure, but I primarily code in PySide (Python bindings for Qt), so do you mind if the code is in Python/pseudocode? More so glad to help out. – Alex Huszagh Jan 03 '16 at 23:02
  • As long as the API is the same, please go ahead. – spraff Jan 06 '16 at 19:15

0 Answers0