Search code examples
qtcontextmenuqdockwidget

How to disable the context menu of a QDockWidget title bar


I have a couple of QDockWidgets that are all not closabale (using Qt 5.6). Therefore, the context menu that is displayed when right-clicking a title bar of one of them only has disabled entries, and I would like to disable the whole context menu.

I tried to set the contextMenuPolicy to NoContextMenu without success.

I then tried to use a subclass of QDockWidget, override the ContextMenuEvent and ignore it. The menu is still displayed.

I then tried to install an event filter to catch the ContextMenuEvent, but it did not catch any, just PaintEvents, ResizeEvents etc.

I'm out of ideas … any help would be greatly appreciated!


Solution

  • As per the comments it is necessary to set the context menu policy on the QDockWidget to Qt::PreventContextMenu...

    dock_widget->setContextMenuPolicy(Qt::PreventContextMenu);
    

    rather than simply Qt::NoContextMenu. From the documentation Qt::NoContextMenu simply defers the context menu handling to the parent widget rather than preventing it entirely.