Search code examples
qtqmenubarqwizardpage

How to add QMenuBar to QWizardPage?


I am trying to add a QMenuBar to QWizardPage

QMenuBar *menuBar = new QMenuBar;
menuBar->setNativeMenuBar(false);
QMenu *helpMenu = new QMenu;
QAction *helpAction = new QAction;

helpMenu->addAction(helpAction);
menuBar->addMenu(helpMenu);

layout->addWidget(menuBar);
//Other widgets
setLayout(layout);

But I can't see the menu bar.

Basically I want to add an "Help" menu with "About product" item to display the product version and licensing information that we generally see in many applications. I am on Windows 10 using QT 5.13.2


Solution

  • The solution is to use a QMainWindow where you set the QMenuBar and use the QWizard as centralWidget:

    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QMainWindow w;
        // menubar
        QMenuBar * menuBar = w.menuBar();
        QMenu *helpmenu = menuBar->addMenu("Help");
        QAction *aboutaction = helpmenu->addAction("About product");
        QObject::connect(aboutaction, &QAction::triggered, [&w](){
            QMessageBox::information(&w, "About", "About");
        });
    
        QWizard *wizard = new QWizard;
    
        // add pages
        wizard->addPage(new QWizardPage);
        wizard->addPage(new QWizardPage);
    
        w.setCentralWidget(wizard);
        w.show();
        return a.exec();
    }