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;
    w.resize(400,400);
    w.show();

    QPushButton * push = new QPushButton("button",&w);
    push->move(100,100);
    push->show();
    
    return
    

    }@

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

    @ QMenu * menu1=new QMenu("menu1",&w);
    menu1->move(100,100);
    menu1->show();@

    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);
    mbar->addMenu(menu1);
    mbar->show();@

    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
 

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