Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]menuBar() not found?



  • I was making a small test program, and came across an error that said that it can't find menuBar. here is the following code:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QtWidgets>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QMainWindow win;
    QMenu *menu = menuBar()->addMenu(tr("&File"));
    menu->addSeparator();
    QHBoxLayout *box = new QHBoxLayout;
    box->addWidget(menu);
    win.setLayout(box);

    return a.exec();
    

    }
    any help is greatly appreciated



  • @manny said:
    You are calling global menuBar() function which does not exist

    QMenu *menu = menuBar()->addMenu(tr("&File"));

    you probably wanted

    QMenu *menu = win.menuBar()->addMenu(tr("&File"));



  • ok, i think i understand



  • sorry, but can someone also explain what is the difference between:

    action = menu->addSeparator()

    and:

    menu->addSeparator()

    I understand that in the first one action is assigned to separator, however they both do the same job, so what is the advantage of using one over the other. Is it maybe in the first one you can possibly hide the separator if you wanted to, or do something else to it?

    any help is, again, greatly appreciated


  • Lifetime Qt Champion

    Yes in
    action newaction= menu->addSeparator()
    you get newaction to set other properties on if needed, where as
    menu->addSeparator()
    you have no returned action to play with.



  • thanks for clearing that up


Log in to reply