Tobias Leupold
Tobias Leupold

Reputation: 1754

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!

Upvotes: 6

Views: 2791

Answers (1)

G.M.
G.M.

Reputation: 12909

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.

Upvotes: 13

Related Questions