6

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!

Tobias Leupold
  • 1,512
  • 1
  • 16
  • 41
  • Have you tried just setting the [context menu policy](http://doc.qt.io/qt-5/qwidget.html#contextMenuPolicy-prop)? – G.M. Jun 17 '17 at 07:09
  • Yes, I also tried this without success … I just forgot to write it up there ;-) – Tobias Leupold Jun 17 '17 at 07:32
  • Please edit your question to show what you've tried so far. Setting the dock widget's context menu policy to [`Qt::PreventContextMenu`](http://doc.qt.io/qt-5/qt.html#ContextMenuPolicy-enum) works for me. – G.M. Jun 17 '17 at 07:39
  • That's it! I set it to Qt::NoContextMenu, and that didn't work, so I thought this wasn't possible this way. Thanks for the hint! Do you want to post it as an answer so that I can accept it? – Tobias Leupold Jun 17 '17 at 07:48

1 Answers1

12

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.

G.M.
  • 12,232
  • 2
  • 15
  • 18