Accessing a (sub)menu defined in an XmlGui ui.rc file

Thomas Baumgart thb at net-bembel.de
Wed Jan 13 10:25:18 GMT 2021


On Mittwoch, 13. Januar 2021 10:39:48 CET Tobias Leupold wrote:

> Hi list :-)
> 
> I have a problem with a KXmlGuiWindow and I honestly can't find respective
> docs or howtos/tutorials. I hope someone here can help me ...
> 
> It's a allegedly basic task: How can I access a submenu (or better: a
> submenu's QAction) for a menu defined in a ...ui.rc file?
> 
> If I define the submenu like so:
> 
>     <MenuBar>
>       <Menu name="foo" >
>         <Menu name="bar">
>           <text>Some text</text>
>           <Action name="someAction"/>
>         </Menu>
>       </Menu>
>     </MenuBar>
> 
> I can access the "someAction" action like the other ones like so:
> 
>     auto *someAction
>         = actionCollection()->addAction(QStringLiteral("someAction"));
>     someAction->setText(i18n("Some action"));
>     someAction->setIcon(QIcon::fromTheme(QStringLiteral("some-icon")));
>     connect(someAction, &QAction::triggered, this, 
> &SomeClass::someFunction);
> 
> and so on.
> 
> But how can I get the "bar" QMenu (or it's respective QAction)? So that I 
> can
> e. g. disable or hide the menu, or assign an icon? For manually defined 
> menus,
> this would be as easy as "auto *action = menu->menuAction();" ...

Here's how it is done in KMyMoney (may not be the best way, but it's working):

  https://invent.kde.org/office/kmymoney/-/blob/master/kmymoney/kmymoney.cpp#L1270

creates all the menus during initialization. The QStringLiterals contain the names
used in the xmlgui.rc file. KMyMoneyApp is derived from KXmlGuiWindow and thus provides
the factory() method. The actions need to exist before the menu is initialized from
what I see. All this is called before setupGUI() is executed.


The context menu can then be executed by e.g.

  lutMenus[Menu::Institution]->exec(QCursor::pos());

Hope that helps and gives you some ideas.

-- 

Regards

Thomas Baumgart

https://www.signal.org/       Signal, the better WhatsApp
-------------------------------------------------------------
Idle servers are the devil's work -- robbyoconnor on #irc
-------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 868 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-devel/attachments/20210113/acf6dabd/attachment.sig>


More information about the kde-devel mailing list