[Qt-interest] How does one dynamically modify a menu?
K. Frank
kfrank29.c at gmail.com
Tue Feb 8 18:07:57 CET 2011
Hello André!
Thank you for the additional suggestion.
On Tue, Feb 8, 2011 at 5:37 AM, Andre Somers <andre at familiesomers.nl> wrote:
> Op Di, 8 februari, 2011 11:11 am, schreef Oliver.Knoll at comit.ch:
>> (...)
>> So I understand you want to be able to show two completely different menu
>> contents attached to the same top-level menu entry, and no (or just very
>> few) menu entries (QAction) "overlap".
>>
>> You could still create ONE QMenu, add ALL QActions, and set the visibility
>> of those QActions accordingly, depending on your application state.
This is more nearly what I want to do. It obviously depends on how
much overlap there is in the two versions of the menu, but in my use
case there is some overlap, so creating two menus has some
duplicated effort for the menu items they have in common.
>> Or you really instantiate two distinct QMenu instances (if those menus
>> really don't contain the same functionality) and add both to the menu bar
>> with http://doc.qt.nokia.com/latest/qmenubar.html#addMenu. Then you switch
>> the visibility on the entire QMenu instead of on the single QActions, that
>> should work, too.
This is what I am currently doing, and it works well (although there is
some duplicated effort).
> If the case is as stated above: two menu's with the same name, but to a
> large extend different contents, I'd go for the second approach. *Much*
> easier to maintain, I think. Instead of having to toggle the visibility of
> a large host of actions, you only need to toggle the visibility two menus.
> That means you also can not forget to add a menu item to the code that
> manages the visibility if you decide to add a menu item later on.
Yes, I don't want to toggle the visibility of individual menu items,
for the reasons you give
> However, also check QActionGroup. You can add QActions to a QActionGroup,
> managing their visibility and other properties together.
This sounds like the best way for my use case. I hadn't realized
that I could call setVisibile() on a QActionGroup.
> This would allow
> you to do the following:
> Create a single menu to which you add all actions: 1) the actions that are
> always available, 2) the actions that are only available in state A, and
> 3) the actions that are only available in state B. Now, add the actions
> under 2) to one QActionGroup, and those under 3) to another QActionGroup.
> You can now manipulate the contents of your menu by just toggeling the
> visibility of these action groups based on the state of your application.
I have tried this scheme out, and it works great and is probably the
least work for my use case.
I use Designer to add a single copy of my "two-version" menu to the
menu bar, and, as you suggest, add to it all of the menu items (items
common to both versions, version-1 items, and version-2 items).
This gives me the convenience of using Designer to do much of the gui
design work.
Then in code I add the relevant menu items to the appropriate
QActionGroups, and hook up the actions to their corresponding
slots.
And when I switch "views" as it were, I toggle the visibility of the
two QActionGroups, thus giving me the appearance of two versions
of the menu, including the items common to both.
> Perhaps the above would be the most elegant solution.
Yes, this works great for what I want to do. It looks like the most
concise way to handle my use case.
> André
Thanks to everyone for their help.
K. Frank.
More information about the Qt-interest-old
mailing list