[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:       Tobias Leupold <tobias.leupold () gmx ! de>
Date:       2021-01-13 16:21:47
Message-ID: 1661891.QkHrqEjB74 () ginuog
[Download RAW message or body]

I still don't get it :-D All I get is nullptrs ...

Maybe I'm missing the forest for the trees?!

I now attached a minimal example showing my dilemma. After having put
demoui.rc to ~/.local/share/kxmlgui5/demo/, the GUI is setup nicely and all,
but how do I get a pointer to the "subMenu" menu? To e. g. assign an icon to
it's action or to disable the whole menu?!

Am Mittwoch, 13. Januar 2021, 14:58:25 CET schrieb Ismael Asensio:
> Once you have the main QAction, it has a menu() method, which in turn has
> an actions() method.
>
> So for instance:
>  bar = foo->menu()->actions().at(0).
>
> Of course with much more logic to check that every item exists and not
> hardcoding the position, but I hope you get the point.

["CMakeLists.txt" (CMakeLists.txt)]

cmake_minimum_required(VERSION 3.8.0)
project(demo CXX)
find_package(ECM 1.1.0 REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(Qt5 5.11 COMPONENTS Widgets REQUIRED)
find_package(KF5 COMPONENTS XmlGui REQUIRED)
set(CMAKE_AUTOMOC ON)
add_executable(demo ${CMAKE_SOURCE_DIR}/main.cpp)
target_link_libraries(demo Qt5::Widgets KF5::XmlGui)

["demoui.rc" (demoui.rc)]

<?xml version="1.0" encoding="UTF-8"?>
<gui name="demo"
     version="1"
     xmlns="http://www.kde.org/standards/kxmlgui/1.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd">
  <MenuBar>
    <Menu name="file">
      <Action name="topMenuAction"/>
      <Menu name="subMenu">
        <text>Sub menu</text>
        <Action name="subMenuAction"/>
      </Menu>
    </Menu>
  </MenuBar>
</gui>

["main.cpp" (main.cpp)]

#include <QApplication>
#include <KXmlGuiWindow>
#include <KActionCollection>

class MainWindow : public KXmlGuiWindow
{
    Q_OBJECT

public:
    explicit MainWindow()
    {
        auto *topMenuAction = actionCollection()->addAction("topMenuAction");
        topMenuAction->setText("Some top menu action");
        topMenuAction->setIcon(QIcon::fromTheme("document-new"));

        auto *subMenuAction = actionCollection()->addAction("subMenuAction");
        subMenuAction->setText("Some sub menu action");
        subMenuAction->setIcon(QIcon::fromTheme("document-open"));

        // How do I get a pointer to the "subMenu" menu?
        // To e. g. assign an icon to it's action or to disable the whole menu?!

        KStandardAction::quit(this, &QWidget::close, actionCollection());

        setupGUI(Keys | Save | Create, "demoui.rc");
    }

};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto *mainWindow = new MainWindow;
    mainWindow->show();
    return app.exec();
}

#include "main.moc"


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

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