[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Accessing a (sub)menu defined in an XmlGui ui.rc file
From:       Thomas Baumgart <thb () net-bembel ! de>
Date:       2021-01-13 10:25:18
Message-ID: 5679583.UjTJXf6HLC () thb-nb
[Download RAW message or body]


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
-------------------------------------------------------------

["signature.asc" (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic