6

I have a map application and a submenu which has dynamically added objects (i.e. points on a map) added to the submenu, depending on the layer that is loaded. I have the ability to hide each individual objects (i.e. a point) by clicking on the corresponding submenu item. Is there any way to organize the submenu? When there are a lot of points (i.e. 100) the entire submenu takes up the screen. Can I add a scrollbar to a submenu? I looked in the documentation, but couldn't find anything that support this feature.

jim
  • 61
  • 1
  • 2

4 Answers4

15

From this bug report I was able to find out that you could do the following:

submenu->setStyleSheet("QMenu { menu-scrollable: 1; }");

Works like a charm.

user2045149
  • 151
  • 1
  • 3
  • This appears to have stopped working in Qt 6 (it does not work for me in Qt 6.4). What does work is creating a custom `QProxyStyle` and setting the sub-menu's style to it. See the example in https://doc.qt.io/qt-6/qproxystyle.html#details (but return true on the `QStyle::SH_Menu_Scrollable` hint - also, `QWidget::setStyle` does not transfer ownership, so their example has a memory leak) – jtooker Jul 28 '23 at 19:10
0

There is no such possibility as far as I know. Maybe you shouldn't use a sub menu for this but prefer a menu entry that show a Point manager GUI of your own which would have a QListWidget displaying all your points items. I'm aware that this solution will break a (big?) part of your code but I don't see anything else.

Jeannot
  • 1,165
  • 7
  • 18
0

I think you may be able to get the effect you want by creating and using your own QStyle subclass (via QApplication::setStyle()), and overriding the styleHint virtual method to return 1 when the StyleHint parameter passed in is SH_Menu_Scrollable. At least, that works for me when I create large QMenu objects and show them as popup menus.... It may also work for QMenus attached to the menu bar, but I havent tried that.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
-1

Whilst it is possible by subclassing the QMenu class to create a custom widget and going from there you're better off looking at a better way to display that information. You'll save yourself time and it'll be much easier for your users to not have to scroll through a big list of items in a small area.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55