Search code examples
c++qtqapplication

How to add an action item in a QT/C++ application


I have created an application in Qt/C++, by default, I have in my main :

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainUI MyWindow(*MyData);
    MyWindow.show();
    return app.exec();
}

MainUi is a class which is used to declare what is supposed to be displayed inside the QApplication.

PulsMainUI::PulsMainUI(MyData& mydata) :
    m_data(mydata)
{
    setWindowTitle(QString::fromUtf8("Browser"));
    resize(800,600);
    setUnifiedTitleAndToolBarOnMac(true);

    QWidget *TopBarWidget = new QWidget();
    TopBarWidget->setFixedHeight(61);
    QHBoxLayout *TopBarLayout = new QHBoxLayout(TopBarWidget);
    ...

    /*about*/
    aboutAction = new QAction(tr("&About"),this);
    connect(aboutAction, SIGNAL(triggered()),this ,SLOT(aboutPuls()));

    /*Overall Layout*/
    QWidget *MainWindowWidget = new QWidget();
    MainWindowWidget->addAction(aboutAction);

    QVBoxLayout *MainWindowLayout = new QVBoxLayout(MainWindowWidget);
    MainWindowLayout->setSpacing(0);
    ...
    setCentralWidget(MainWindowWidget);
    show();
}

This part is working fine apart of

aboutAction = new QAction(tr("&About"),this);
connect(aboutAction, SIGNAL(triggered()),this ,SLOT(aboutPuls()));
MainWindowWidget->addAction(aboutAction);

My goal is not to create a new menu but just a new action in the default available menu. By default, QApplication create a menu 'puls_connect', it's the name of my project with the "Exit" action. I just want to add inside the "About"

enter image description here

Any idea ?

Thanks


Solution

  • You need to create a top level Help menu and add About action into it. Qt will automatically move it to the main application menu on OS X.

    An example:

    QMenu *helpMenu = new QMenu("Help", this);
    helpMenu->addAction("About");
    menuBar()->addMenu(helpMenu);