Custom Qt QMenu

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

The example below (left) is what I have, and I would like to aim for something similar to the layout on the right, adding non-menu widgets. If this cannot be done with QMenu , are there guides to get similar results (possibly using a more standard widget as a context menu)?

Custom QMenu

+8
qt qmenu custom-contextmenu
source share
2 answers

Of course have! In Qt, if there is a will, there is a way.

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

Then you need to generate the layout and overload all the correct QLayout functions to recalculate the size.

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

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

EDIT:

As the commentator noted, you cannot inherit two QObject things, so I updated the answer accordingly.

+8
source share

To customize menu items, you can use the QWidgetAction class. But you want to customize the menu so that it looks like a popup widget. This way you can subclass QMenu and try to improve the menu layout for your needs ( QMenu is QWidget ). You do not understand what you are looking for.

0
source share

All Articles