Important: Please read the Qt Code of Conduct -

Problem with reimplementation of event() in QTableView subclass

  • I am developing a cross-platform application which should also work on tablets. There is a couple of context menus (created using Qt::ActionsContextMenu policy) and I want them to appear on tablets by tap and hold gesture.

    In one case the menu belongs to a custom widget subclassed from QWidget. I have reimplemented the event handler in the following way:

    @bool MyWidget::event(QEvent event)
    if (event->type() == QEvent::Gesture)
    gEvent = static_cast<QGestureEvent*>(event);
    if (gEvent->gesture(Qt::TapAndHoldGesture) != NULL)
    QTapAndHoldGesture gesture =
    if (gesture->state() == Qt::GestureFinished)
    QContextMenuEvent menuEvent(QContextMenuEvent::Other,
    QApplication::sendEvent(this, &menuEvent);
    return true;
    else return QWidget::event(event);

    It works as expected, no problems.

    Another menu belongs to QTableView. I have subclassed it and reimplemented the event handler in the same way, except for the last line:

    @return QTableView::event(event);@

    I does not work at all, the menu is never shown, despite the fact that the QContextMenuEvent event is sent. If I change QTableView::event(event) call to QWidget::event(event), the menu is shown, but, obviously, some parts of the table are not rendered, because some paint events are not processed. Any ideas?

  • After some experiments I have found that changing the last line to these two does the job:

    @else if (event->type() != QEvent::ContextMenu)
    return QTableView::event(event);
    return QWidget::event(event);@

    Still, I do not understand why QTableView does not process ContextMenu events.

Log in to reply