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



  • 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.


  • Lifetime Qt Champion

    Hi,

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



  • @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.


  • Lifetime Qt Champion

    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());    
    }
    

    ?



  • @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.


Log in to reply
 

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