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. Adding a QMenuBar to a ui...
Forum Updated to NodeBB v4.3 + New Features

Adding a QMenuBar to a ui...

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 391 Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I'm following:
    https://www.youtube.com/watch?v=E9eQO5czNrw

    My ui file:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>clsMainWnd</class>
     <widget class="QMainWindow" name="clsMainWnd">
      <property name="windowModality">
       <enum>Qt::NonModal</enum>
      </property>
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>196</width>
        <height>137</height>
       </rect>
      </property>
      <property name="focusPolicy">
       <enum>Qt::NoFocus</enum>
      </property>
      <property name="windowTitle">
       <string notr="true"/>
      </property>
      <property name="toolTip">
       <string notr="true"/>
      </property>
      <property name="statusTip">
       <string notr="true"/>
      </property>
      <widget class="QWidget" name="centralWidget">
       <property name="styleSheet">
        <string notr="true">background-color:#ff333333</string>
       </property>
       <widget class="QLabel" name="lblHdr">
        <property name="geometry">
         <rect>
          <x>7</x>
          <y>0</y>
          <width>161</width>
          <height>38</height>
         </rect>
        </property>
        <property name="text">
         <string notr="true"/>
        </property>
        <property name="pixmap">
         <pixmap resource="clsmainwnd.qrc">:/CMPY_LOGO</pixmap>
        </property>
        <property name="scaledContents">
         <bool>false</bool>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblAppTitle">
        <property name="geometry">
         <rect>
          <x>8</x>
          <y>40</y>
          <width>161</width>
          <height>20</height>
         </rect>
        </property>
        <property name="toolTip">
         <string>XML Multi-Platform Application Manager</string>
        </property>
        <property name="styleSheet">
         <string notr="true">color:#ffffffff;</string>
        </property>
        <property name="text">
         <string>XMLMPAM</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
        <property name="openExternalLinks">
         <bool>true</bool>
        </property>
       </widget>
       <widget class="QLabel" name="lblWidth">
        <property name="geometry">
         <rect>
          <x>8</x>
          <y>80</y>
          <width>91</width>
          <height>20</height>
         </rect>
        </property>
        <property name="styleSheet">
         <string notr="true">color:#ffffffff;</string>
        </property>
        <property name="text">
         <string>Display width: </string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblHeight">
        <property name="geometry">
         <rect>
          <x>8</x>
          <y>60</y>
          <width>91</width>
          <height>20</height>
         </rect>
        </property>
        <property name="styleSheet">
         <string notr="true">color:#ffffffff;</string>
        </property>
        <property name="text">
         <string>Display height:</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblWidthVal">
        <property name="geometry">
         <rect>
          <x>110</x>
          <y>80</y>
          <width>60</width>
          <height>20</height>
         </rect>
        </property>
        <property name="styleSheet">
         <string notr="true">color:#ffffffff;</string>
        </property>
        <property name="text">
         <string>0</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblHeightVal">
        <property name="geometry">
         <rect>
          <x>110</x>
          <y>60</y>
          <width>60</width>
          <height>20</height>
         </rect>
        </property>
        <property name="styleSheet">
         <string notr="true">color:#ffffffff;</string>
        </property>
        <property name="text">
         <string>0</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="mnuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>196</width>
         <height>24</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>File</string>
        </property>
        <addaction name="actionNew"/>
        <addaction name="actionOpen"/>
        <addaction name="actionExit"/>
       </widget>
       <addaction name="menuFile"/>
      </widget>
      <action name="actionNew">
       <property name="text">
        <string>New</string>
       </property>
       <property name="shortcut">
        <string>Ctrl+N</string>
       </property>
      </action>
      <action name="actionOpen">
       <property name="text">
        <string>Open</string>
       </property>
       <property name="shortcut">
        <string>Ctrl+O</string>
       </property>
      </action>
      <action name="actionExit">
       <property name="text">
        <string>Exit</string>
       </property>
       <property name="shortcut">
        <string>Ctrl+X</string>
       </property>
      </action>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources>
      <include location="clsmainwnd.qrc"/>
     </resources>
     <connections/>
    </ui>
    

    Whilst I can see the menu and the submenu in Qt Creator, when I build and launch the application I am not seeing the menu bar or the menus.

    This is my class constructor:

    clsMainWnd::clsMainWnd(QWidget* pobjParent) : QMainWindow(pobjParent)
                                                , ui(new Ui::clsMainWnd) {
    //Check that window doesn't already exist
        Q_ASSERT_X(mspobjAppWnd==nullptr, "clsMainWnd", "mspobjAppWnd");
    //Modify the window to occupy full screen with no window dressing
    //    setWindowFlags(Qt::CustomizeWindowHint
    //                 | Qt::FramelessWindowHint
    //                 | Qt::WindowStaysOnTopHint);
    //Get desktop geometry   
        msrctDesktop = screen()->geometry();
    //Save pointer to main window
        mspobjAppWnd = this;
    //Set-up the user interface
        ui->setupUi(this);
        QString strTemp;
        strTemp = QString::asprintf("%dpx", msrctDesktop.width());
        ui->lblWidthVal->setText(static_cast<const QString>(strTemp));
        strTemp = QString::asprintf("%dpx", msrctDesktop.height());
        ui->lblHeightVal->setText(static_cast<const QString>(strTemp));
    //Set-up one second timer to perform house keeping functions
        QTimer::singleShot(REMOVE_SPLASH_TIME, this, &clsMainWnd::onRemoveSplash);
    //Start socket server thread to listen to modules
    //This application is the socket server, when modules are started they
    //will send a message to this application via the server socket
        mspsckServer = new clsListener(clsListener::uint16NextPort(), this);
    }
    

    This is just a demo, once its working I have other plans.

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @jsulm , I think I've found it, it could be this code in the constructor that Is changing the geometry and probably displaying the labels over the menu:

          ui->setupUi(this);
          QString strTemp;
          strTemp = QString::asprintf("%dpx", msrctDesktop.width());
          ui->lblWidthVal->setText(static_cast<const QString>(strTemp));
          strTemp = QString::asprintf("%dpx", msrctDesktop.height());
          ui->lblHeightVal->setText(static_cast<const QString>(strTemp));
      
      

      [Edit] no that wasn't it either.

      And obviously I read it completely wrong, that simply sets the label text.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @SPlatten In MacOS the menu of every application is in the top bar, not in the application window itself.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS 1 Reply Last reply
      2
      • SPlattenS SPlatten

        I'm following:
        https://www.youtube.com/watch?v=E9eQO5czNrw

        My ui file:

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>clsMainWnd</class>
         <widget class="QMainWindow" name="clsMainWnd">
          <property name="windowModality">
           <enum>Qt::NonModal</enum>
          </property>
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>196</width>
            <height>137</height>
           </rect>
          </property>
          <property name="focusPolicy">
           <enum>Qt::NoFocus</enum>
          </property>
          <property name="windowTitle">
           <string notr="true"/>
          </property>
          <property name="toolTip">
           <string notr="true"/>
          </property>
          <property name="statusTip">
           <string notr="true"/>
          </property>
          <widget class="QWidget" name="centralWidget">
           <property name="styleSheet">
            <string notr="true">background-color:#ff333333</string>
           </property>
           <widget class="QLabel" name="lblHdr">
            <property name="geometry">
             <rect>
              <x>7</x>
              <y>0</y>
              <width>161</width>
              <height>38</height>
             </rect>
            </property>
            <property name="text">
             <string notr="true"/>
            </property>
            <property name="pixmap">
             <pixmap resource="clsmainwnd.qrc">:/CMPY_LOGO</pixmap>
            </property>
            <property name="scaledContents">
             <bool>false</bool>
            </property>
            <property name="alignment">
             <set>Qt::AlignCenter</set>
            </property>
           </widget>
           <widget class="QLabel" name="lblAppTitle">
            <property name="geometry">
             <rect>
              <x>8</x>
              <y>40</y>
              <width>161</width>
              <height>20</height>
             </rect>
            </property>
            <property name="toolTip">
             <string>XML Multi-Platform Application Manager</string>
            </property>
            <property name="styleSheet">
             <string notr="true">color:#ffffffff;</string>
            </property>
            <property name="text">
             <string>XMLMPAM</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignCenter</set>
            </property>
            <property name="openExternalLinks">
             <bool>true</bool>
            </property>
           </widget>
           <widget class="QLabel" name="lblWidth">
            <property name="geometry">
             <rect>
              <x>8</x>
              <y>80</y>
              <width>91</width>
              <height>20</height>
             </rect>
            </property>
            <property name="styleSheet">
             <string notr="true">color:#ffffffff;</string>
            </property>
            <property name="text">
             <string>Display width: </string>
            </property>
            <property name="alignment">
             <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
            </property>
           </widget>
           <widget class="QLabel" name="lblHeight">
            <property name="geometry">
             <rect>
              <x>8</x>
              <y>60</y>
              <width>91</width>
              <height>20</height>
             </rect>
            </property>
            <property name="styleSheet">
             <string notr="true">color:#ffffffff;</string>
            </property>
            <property name="text">
             <string>Display height:</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
            </property>
           </widget>
           <widget class="QLabel" name="lblWidthVal">
            <property name="geometry">
             <rect>
              <x>110</x>
              <y>80</y>
              <width>60</width>
              <height>20</height>
             </rect>
            </property>
            <property name="styleSheet">
             <string notr="true">color:#ffffffff;</string>
            </property>
            <property name="text">
             <string>0</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
            </property>
           </widget>
           <widget class="QLabel" name="lblHeightVal">
            <property name="geometry">
             <rect>
              <x>110</x>
              <y>60</y>
              <width>60</width>
              <height>20</height>
             </rect>
            </property>
            <property name="styleSheet">
             <string notr="true">color:#ffffffff;</string>
            </property>
            <property name="text">
             <string>0</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
            </property>
           </widget>
          </widget>
          <widget class="QMenuBar" name="mnuBar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>196</width>
             <height>24</height>
            </rect>
           </property>
           <widget class="QMenu" name="menuFile">
            <property name="title">
             <string>File</string>
            </property>
            <addaction name="actionNew"/>
            <addaction name="actionOpen"/>
            <addaction name="actionExit"/>
           </widget>
           <addaction name="menuFile"/>
          </widget>
          <action name="actionNew">
           <property name="text">
            <string>New</string>
           </property>
           <property name="shortcut">
            <string>Ctrl+N</string>
           </property>
          </action>
          <action name="actionOpen">
           <property name="text">
            <string>Open</string>
           </property>
           <property name="shortcut">
            <string>Ctrl+O</string>
           </property>
          </action>
          <action name="actionExit">
           <property name="text">
            <string>Exit</string>
           </property>
           <property name="shortcut">
            <string>Ctrl+X</string>
           </property>
          </action>
         </widget>
         <layoutdefault spacing="6" margin="11"/>
         <resources>
          <include location="clsmainwnd.qrc"/>
         </resources>
         <connections/>
        </ui>
        

        Whilst I can see the menu and the submenu in Qt Creator, when I build and launch the application I am not seeing the menu bar or the menus.

        This is my class constructor:

        clsMainWnd::clsMainWnd(QWidget* pobjParent) : QMainWindow(pobjParent)
                                                    , ui(new Ui::clsMainWnd) {
        //Check that window doesn't already exist
            Q_ASSERT_X(mspobjAppWnd==nullptr, "clsMainWnd", "mspobjAppWnd");
        //Modify the window to occupy full screen with no window dressing
        //    setWindowFlags(Qt::CustomizeWindowHint
        //                 | Qt::FramelessWindowHint
        //                 | Qt::WindowStaysOnTopHint);
        //Get desktop geometry   
            msrctDesktop = screen()->geometry();
        //Save pointer to main window
            mspobjAppWnd = this;
        //Set-up the user interface
            ui->setupUi(this);
            QString strTemp;
            strTemp = QString::asprintf("%dpx", msrctDesktop.width());
            ui->lblWidthVal->setText(static_cast<const QString>(strTemp));
            strTemp = QString::asprintf("%dpx", msrctDesktop.height());
            ui->lblHeightVal->setText(static_cast<const QString>(strTemp));
        //Set-up one second timer to perform house keeping functions
            QTimer::singleShot(REMOVE_SPLASH_TIME, this, &clsMainWnd::onRemoveSplash);
        //Start socket server thread to listen to modules
        //This application is the socket server, when modules are started they
        //will send a message to this application via the server socket
            mspsckServer = new clsListener(clsListener::uint16NextPort(), this);
        }
        

        This is just a demo, once its working I have other plans.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @SPlatten Your ui files works fine for me on Linux. On what OS are you?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 2 Replies Last reply
        0
        • jsulmJ jsulm

          @SPlatten Your ui files works fine for me on Linux. On what OS are you?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @jsulm

          macOS Monterey Version 12.7.1

          Qt Creator 11.0.3
          Based on Qt 6.4.3 (Clang 13.0 (Apple), x86_64)
          Built on Sep 27 2023 06:47:32

          I'm using 5.15.2

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @SPlatten Your ui files works fine for me on Linux. On what OS are you?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #4

            @jsulm , I think I've found it, it could be this code in the constructor that Is changing the geometry and probably displaying the labels over the menu:

                ui->setupUi(this);
                QString strTemp;
                strTemp = QString::asprintf("%dpx", msrctDesktop.width());
                ui->lblWidthVal->setText(static_cast<const QString>(strTemp));
                strTemp = QString::asprintf("%dpx", msrctDesktop.height());
                ui->lblHeightVal->setText(static_cast<const QString>(strTemp));
            
            

            [Edit] no that wasn't it either.

            And obviously I read it completely wrong, that simply sets the label text.

            Kind Regards,
            Sy

            jsulmJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @jsulm , I think I've found it, it could be this code in the constructor that Is changing the geometry and probably displaying the labels over the menu:

                  ui->setupUi(this);
                  QString strTemp;
                  strTemp = QString::asprintf("%dpx", msrctDesktop.width());
                  ui->lblWidthVal->setText(static_cast<const QString>(strTemp));
                  strTemp = QString::asprintf("%dpx", msrctDesktop.height());
                  ui->lblHeightVal->setText(static_cast<const QString>(strTemp));
              
              

              [Edit] no that wasn't it either.

              And obviously I read it completely wrong, that simply sets the label text.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @SPlatten In MacOS the menu of every application is in the top bar, not in the application window itself.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              SPlattenS 1 Reply Last reply
              2
              • jsulmJ jsulm

                @SPlatten In MacOS the menu of every application is in the top bar, not in the application window itself.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #6

                @jsulm Doh!

                Of course!, you are correct and it does work, I wasn't looking for it there.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS SPlatten has marked this topic as solved on

                • Login

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