qtqmenucustom-contextmenu

Custom Qt QMenu


Is there a way in Qt to add a layout or widgets to a QMenu to create a custom menu?

The example below (left) is what I have, and I'd like to aim for something similar to the mock-up on the right, by adding non-menu widgets. If it can't be done by QMenu, are there guides to produce similar results (perhaps by having a more standard widget act as a context menu) anywhere?

Custom QMenu


Solution

  • Sure there is! In Qt, if there is a a will there is a way.

    You will probably need to make your own class that uses QMenu and uses a member QListWidget.

    You will then need to generate the layout and overload all the correct QLayout functions for size recalculation.

    And then you'll need to use this layout (think QHBoxLayout) to display both a QMenu and a QListWidget side by side.

    That should be enough to point you in the right direction.

    EDIT:

    As a commenter pointed out, you can't inherit two QObject things so I updated the answer accordingly.