Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I build a QMenu bar and then assign it to a widget?
Qt 6.12 LTS is out! See what's new in the release blog

How do I build a QMenu bar and then assign it to a widget?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.5k 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by graniteDev
    #1

    I have several applications that all use the same exact menu across the top. Right now I've made the menu in the standard way like so:

    QMenu * utilMenu = menuBar()->addMenu("Utilities");
    QMenu * envMenu = menuBar()->addMenu("Environment");
    QMenu * devMenu = menuBar()->addMenu("Dev Tools");
    
    QAction *networkAction = new QAction("Network",this);
    QAction *deviceViewer = new QAction("Device Version Viewer", this);
    
    networkAction->setCheckable(false);
    deviceViewer->setCheckable(false);
    
    utilMenu->addAction(networkAction);
    utilMenu->addAction(deviceViewer);
    utilMenu->addSeparator();
    
    connect(networkAction,SIGNAL(triggered()),this,SLOT(onNetworkAction()));
    connect(deviceViewer,SIGNAL(triggered()),this,SLOT(onDeviceViewer()));
    
    //and so on and so forth for each QMenu until they are all populated
    

    I do this for all the menu items, everything works just fine. The issue is, I have this exact code, in all my applications. This is not DRY at all, and any time a feature is added or removed from the menu I've got the change this thing in multiple locations.

    I have various libraries of custom utilities and widgets that these applications all share, there has got to be a way to do something similar for setting up a common menubar.

    What I really want is to have an external standard QMenuBar object kept in an external class, that is already set up, and I can just assign each apps menuBar object this standardized menu bar.

    I've been looking over the documentation until my eyes glazed over. Is it even possible to do what I want here? Is there a better way that has escaped me to allow for a shared standard menubar across my applications?

    I've even looked to see if there is a way to copy code verbatim from say a text file at compile time and just dump that text into a function on each app. I don't see a way to do that either.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not have a method to which you pass the menu to populate ? That method could be in your library.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Why not have a method to which you pass the menu to populate ? That method could be in your library.

        G Offline
        G Offline
        graniteDev
        wrote on last edited by
        #3

        @SGaist That's exactly what I want. That's exactly what I tried to do, but I can't find a way to pass that menu to the menuBar of the mainwindow. The documentation is silent on handing menuBar a prebuilt menu for it to use. It only details how to construct a menubar from within the widget itself.

        interestingly, just using an include statement inside the blank function with all the above code worked. It might be an effective work around until I can figure out how to abstract this code into a class.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Something like:

          MyHelpers.cpp

          void populateMenuBar(QMenuBar *menuBar)
          {
          QMenu * utilMenu = menuBar->addMenu("Utilities");
          }
          

          MainWindow.cpp

          #include "MyHelpers.h"
          
          MainWindow(QWidget *parent):
              QMainWindow(parent)
          {
              populateMenuBar(menuBar());    
          }
          

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          1
          • SGaistS SGaist

            Something like:

            MyHelpers.cpp

            void populateMenuBar(QMenuBar *menuBar)
            {
            QMenu * utilMenu = menuBar->addMenu("Utilities");
            }
            

            MainWindow.cpp

            #include "MyHelpers.h"
            
            MainWindow(QWidget *parent):
                QMainWindow(parent)
            {
                populateMenuBar(menuBar());    
            }
            

            ?

            G Offline
            G Offline
            graniteDev
            wrote on last edited by
            #5

            @SGaist Now I feel like an idiot. I was looking in completely the wrong place in the documentation. I didn't even think to look for a function within QMainWindow. I was focusing on QMenu and QMenuBar etc.

            Thank you for pointing out the obvious.

            1 Reply Last reply
            1

            • Login

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