Reputation: 2467
I'd like to create a context menu looking similar to this one:
I read suggestions on the web that QWidget::setMask()
should be used to create a shape. But how can it fit the variable number of items then? Moreover, the same menu item may take more or less screen space on different machines.
Another question is how to create a shadow around this custom shape? As far as I understand, the mask allows to crop the widget, but not to make it semi-transparent.
Upvotes: 0
Views: 766
Reputation: 2881
I don’t found an easy way to do that! But here goes a way!
Instead of using the Qt mask API, I've used a frame-less widget with transparency enabled!
To draw the shadow, I've used radial gradient!
You can change the size of the menu before opening it, however you can’t resize it after opened (for example resize with mouse).
It’s quite easy add or remove widgets, just respect the layout margin to not draw outside the bounds destined to widgets. To simplify your life I created an inherited class of QPushButton
with colors you can easily customize with style sheet.
See the result:
You can browse the source
Hope that helps!
Upvotes: 1