Congratulations to our 2022 Qt Champions!

Help with Isolated QMenu problem

  • I'm having a problem placing an isolated QMenu widget as a child widget. I suspect there is something about QMenu that I don't understand and am looking for help. Since QMenu is a widget I assumed that it could be made a child widget of any parent widget; specifically as part of a main window. The documentation gives a very simple example of adding a child widget to a main window (for a QPushButton) as follows:

    @int main(int argc, char *argv[])
    { QApplication a(argc, argv);
    MainWindow w;

    QPushButton * push = new QPushButton("button",&w);


    This works but when I try this method with QMenu replacing the QPushButton code with

    @ QMenu * menu1=new QMenu("menu1",&w);

    it does not work. (The documentation says to use the exec function but that didn't work either nor did setting visible or enabled to true work). However if I place the menu in a QMenuBar as follows:

    @ QMenu * menu1=new QMenu("menu1",&w);
    QMenuBar * mbar = new QMenuBar(&w);

    both the menu and the menu bar are visible and enabled.

    I know that mbar becomes the parent of menu1 when using the function addMenu (according to the documentation) so perhaps that is why menu1 becomes visible but can anyone explain why if QMenu is a widget then why cannot it be a child widget of a main window? I'm mystified.

Log in to reply