[Qt-interest] How does one dynamically modify a menu?
K. Frank
kfrank29.c at gmail.com
Mon Feb 7 16:51:18 CET 2011
Hello Guys!
Thanks for your pointers.
On Sun, Feb 6, 2011 at 8:37 PM, <noam.rosenthal at nokia.com> wrote:
>
> On Feb 6, 2011, at 5:23 PM, ext K. Frank wrote:
>
>> Hello All!
>>
>> How can I replace one menu with another at run time? (Alternatively,
>> how can I replace menu items in a menu?)
>>
>> I see that QMenuBar has an addMenu function and QMenu has an
>> addAction function, but they have neither a removeMenu nor a a
>> removeAction function, respectively.
>>
>
> QMenu does have a removeAction method, though it doesn't appear in the doc
> since it's not overloaded from QWidget.
> http://doc.qt.nokia.com/4.7/qwidget.html#removeAction
Yes, thank you. I overlooked that. (Although I probably would not
have thought to use it, even if I had seen it.)
> IIRC you can use that function to remove a menu from the menubar
> menuBar->removeAction(menu->menuAction());
Perfect. This removes the menu from the menu bar, and I can
add it back later.
> ~No'am
On Mon, Feb 7, 2011 at 1:04 AM, Tony Rietwyk
<tony.rietwyk at rightsoft.com.au> wrote:
> Hi,
>
> Besides No'am's answer, you hide menu items by changing the visibility of
> the action, not the menu items themselves. You may need to use menuAction()
> for the sub menus generated via Designer.
Yes, menu->menuAction()->setVisible (false) hides the menu.
Thanks.
>
> Tony.
> ...
So what I've done is build two menus, M1 and M2, in my menu bar with
designer, and toggle back and forth between them.
Two schemes work:
void useM1() {
menuM1->menuAction()->setVisible (true);
menuM2->menuAction()->setVisible (false);
}
void useM2() {
menuM1->menuAction()->setVisible (false);
menuM2->menuAction()->setVisible (true);
}
and
void useM1() {
menubar->addMenu (menuM1);
menubar->removeAction (menuM2->menuAction());
}
void useM2() {
menubar->removeAction (menuM1->menuAction());
menubar->addMenu (menuM2);
}
In practice, they both do what I want and seem to behave identically.
I suppose the first (setVisible) would be a little cleaner and is to be
preferred. Is there any particular reason I should choose one over
the other?
But, either way, my problem is solved, so thanks.
On Mon, Feb 7, 2011 at 4:16 AM, <Oliver.Knoll at comit.ch> wrote:
> On 2011-02-07 Noam noam.rosenthal at nokia.com wrote:
>
>> QMenu does have a removeAction method, though it doesn't appear in the
>> doc since it's not overloaded from QWidget.
>
> If you really want to *remove* a menu entry simply deleting the corresponding QAction is enough: the QObject meta-system will automatically make sure that the given QObject (the QAction) is unregistered from its parents (aka "owners") which happens to the the QMenu. So the menu entry is removed for you.
In fact, I don't want to fully destroy (and disconnect and unregister)
the menu. I want to keep it around, fully functional and connected,
but hidden, so that I can reuse it later.
>
> If you want to *change* an existing menu entry you can use the usual QAction#setText() etc. method and disconnect/reconnect the QAction as needed.
In practice, this sounds like it would be more work and more
confusing that toggling the visibility on and off, but I would
like to understand how to do this for my specific use case.
If my "menu entry" is a menu in a menu bar, how would I
disconnect/reconnect the entry to pop up two different menus?
I start with two menus (designer-created) in my menu bar, M1
and M2. Just to be organized, let say I also have a third menu
in my menu bar, M, and I remove M1 and M2 from the menu bar
(but don't destroy or disconnect them). I want clicking on M
in the menu bar to pop up the menu contents of either M1 or M2.
I suppose the QAction in question is M->menuAction(). Is this
correct? How do I connect up this QAction to pop up the desired
menu contents (that of M1 or M2)?
Thanks for any further detail.
> Cheers, Oliver
> --
> Oliver Knoll
> Dipl. Informatik-Ing. ETH
Thanks to all for your help.
K. Frank
More information about the Qt-interest-old
mailing list