[Qt-interest] How does one dynamically modify a menu?
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Tue Feb 8 11:11:04 CET 2011
On 2011-02-07 Frank K. Frank wrote:
>> ...
>> If you really want to *remove* a menu entry simply deleting the corresponding
> QAction is enough ...
> 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.
Then you want to go the "visibility approach" instead :)
My use case currently is that I have a "Window" menu which contains an entry for every opened window. This menu is created/updated dynamically, that is whenever a window is closed its corresponding menu QAction is simply deleted - and the menu entry removed.
>> 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.
Well, it really all depends on your concrete use-case. Seems like you really just want to "toggle" between two "menu states". Setting the visibility is then the way to go then.
> If my "menu entry" is a menu in a menu bar, how would I disconnect/reconnect the
> entry to pop up two different menus?
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.
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.
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list