Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to move the QMenuBar definition in a separate UI file

How to move the QMenuBar definition in a separate UI file

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lygstate
    wrote on last edited by
    #1

    Because I want to generate it by external scripts.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lygstate
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lygstate
            wrote on last edited by
            #5

            It's works, thanks very much!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved