How to move the QMenuBar definition in a separate UI file



  • Because I want to generate it by external scripts.


  • Moderators

    Welcome to devnet.

    There's little point in employing separate .ui file just for menu bar since it's basically a list of QActions. You can store it as such and load at runtime. If it really needs to be in a .ui format there's "QUiLoader":http://qt-project.org/doc/qt-5.0/qtuitools/quiloader.html

    There's no way to sorta #include one ui into another at compile time. But creating a menu is basically a bunch of addMenu () and addAction() calls so maybe your script can generate that.



  • I didn't gotcha your idea.
    can give me a small demo about how to use QUiLoader to load the QMenuBar?


  • Moderators

    Assumming that you have a .ui file in your resources defining a menu bar with File->Open menus like this:
    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MenuBar</class>
    <widget class="QMenuBar" name="MenuBar">
    <widget class="QMenu" name="menuFile">
    <property name="title">
    <string>File</string>
    </property>
    <addaction name="actionOpen"/>
    </widget>
    <action name="actionOpen">
    <property name="text">
    <string>Open</string>
    </property>
    </action>
    <addaction name="menuFile"/>
    </widget>
    <resources/>
    <connections/>
    </ui>
    @
    And you want to load it into your mainwindow you would do so like this:
    @
    QUiLoader loader;
    QFile file(":/form.ui");
    file.open(QFile::ReadOnly);
    QMenuBar* menuBar = qobject_cast<QMenuBar*>(loader.load(&file, this));
    file.close();

    setMenuBar(menuBar);
    @
    Some error checking would be nice of course, but that's the general idea.



  • It's works, thanks very much!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.