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

    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">
    <widget class="QMenuBar" name="MenuBar">
    <widget class="QMenu" name="menuFile">
    <property name="title">
    <addaction name="actionOpen"/>
    <action name="actionOpen">
    <property name="text">
    <addaction name="menuFile"/>
    And you want to load it into your mainwindow you would do so like this:
    QUiLoader loader;
    QFile file(":/form.ui");;
    QMenuBar* menuBar = qobject_cast<QMenuBar*>(loader.load(&file, this));

    Some error checking would be nice of course, but that's the general idea.

  • It's works, thanks very much!

Log in to reply