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

mouseWheel event for QHeaderView of a QTableWidget



  • Hello,

    I have a few custom QTableWidgets in a QHBoxLaout housed in a scrollable view. In the custom QTableWidget, I override the mouseWheel event to filter out the vertical mouse scroll events so they get passed on up to the scrollable view and the UI scrolls between the QTableWidgets rather than attempt to scroll the content of each QTableWidget.

    This all works wonderfully unless the scroll event happens over the horizontal header. This doesn't generate any mouseWheel event, and it also doesn't generate ANY events in eventFilter! So the scroll basically has no effect whatsoever and nothing scrolls one way or another.

    I tried creating a custom QHeaderView with an even filter and setting the horizontal header to it but it also doesn't receive any scroll events whatsoever.

    Does anyone know how to capture (and ignore) the scroll events of a horizontal header of a QTableWidget?

    Thank you! Adam



  • Bump??

    Anyone have an idea about this?


  • Lifetime Qt Champion

    Please provide a minimal, compilable example.



  • I have a custom QTableWidget class with an overloaded wheelEvent:

    void MyTableWidget::wheelEvent(QWheelEvent *event)
    {
        // Pass vertical scrolls to the parent to scroll the list of editors
        // but pass the horizontal scroll to the horizontal scrollbar to scroll this QTableWidget.
        if (event->angleDelta().x() != 0)
        {
            QApplication::sendEvent(this->horizontalScrollBar(), event);
    
            if (event->angleDelta().y() != 0)
            {
                event->ignore();
            }
        }
        else
        {
            event->ignore();
        }
    }
    

    The above code works perfectly except that wheelEvent doesn't get triggered when the mouse is over the header area of a QTableWidget. I haven't managed to track what does get triggered when the scroll even occurs over the header. I would like to pass that scroll event to the parent of the QTableWidget just like in the code above.

    Thank you! Adam


  • Lifetime Qt Champion

    This is no minimal, compilable example but when you want to retrieve the wheelEvent of the header shouldn't you install an event filter on the header then?



  • @Christian-Ehrlicher Thank you Christian,

    I realise it's not a full compilable code and I apologise - it would be a mission to extract the relevant code from this massive app.

    I have tried installing a filter in the MyTableWidget's constructor on the horizontalHeader (QHeaderView) object. The eventFilter does receive the QEvent::Wheel event but I haven't managed to pass it on to either the MyTableWidget itself (presumably to be then caught by the wheelEvent above which does work already) or just return false or do an event->ignore() on it.

    I also tried to do a QApplication::sendEvent(this, wheelEvent) to force forwarding this event to the MyTableWidget but it also doesn't do anything.

    The result is always that nothing scrolls up or down.


  • Lifetime Qt Champion

    @Adam-Crowe said in mouseWheel event for QHeaderView of a QTableWidget:

    The eventFilter does receive the QEvent::Wheel event but I haven't managed to pass it on to either the MyTableWidget itself (presumably to be then caught by the wheelEvent above which does work already) or just return false or do an event->ignore() on it.

    Please show some code... minimal, fully compilable.


Log in to reply