Results 1 to 8 of 8

Thread: Getting QMenuBar of child widget to showup

  1. #1
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question Getting QMenuBar of child widget to showup

    I have an application with a QMainWindow subclass which has a menubar at the top. I have a child widget which is in a dockwidget. The child widget has a QTextEdit in it. It is organized with QVBoxLayout. The problem is that I can't get the menubar to show up.

    I tried this code from qmainwindow.cpp (not sure the const_cast stuff is necessary)
    Qt Code:
    1. MyWidget *self = const_cast<MyWidget*>(this);
    2. mMenuBar = new QMenuBar(self);
    To copy to clipboard, switch view to plain text mode 

    And then:

    Qt Code:
    1. QVBoxLayout *mainLayout = new QVBoxLayout;
    2. mainLayout->setMenuBar(mMenuBar);
    3. mainLayout->addWidget(mTextEdit);
    To copy to clipboard, switch view to plain text mode 

    But it doesn't show up.

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting QMenuBar of child widget to showup

    Your menubar is related to your QMainWindow widget, right ?

    What if you add it like this :

    Qt Code:
    1. mainwindow->setMenuBar(mMenuBar);
    To copy to clipboard, switch view to plain text mode 

    Or maybe, I misunderstood your issue...

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Getting QMenuBar of child widget to showup

    For a main window, menu bar gets created on demand when QMainWindow::menuBar() is called. If you want a separate menu inside the dock widget:
    Qt Code:
    1. QDockWidget* dockWidget = new QDockWidget(mainWindow);
    2. QWidget* wrapper = new QWidget(dockWidget);
    3. QVBoxLayout* layout = new QVBoxLayout(wrapper);
    4. QMenuBar* menuBar = new QMenuBar(wrapper);
    5. QMenu* dockMenu = menuBar->addMenu("Dock menu");
    6. dockMenu->addAction("blaa");
    7. QTextEdit* textEdit = new QTextEdit(wrapper);
    8. layout->setMenuBar(menuBar);
    9. layout->addWidget(textEdit);
    10. dockWidget->setWidget(wrapper);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question Re: Getting QMenuBar of child widget to showup

    Thanks for your help, jpn!

    This is essentially what I have except my subclass of QWidget is where the layout/menu stuff happens. So I have:

    Qt Code:
    1. QVBoxLayout* layout = new QVBoxLayout(this);
    2. QMenuBar* menuBar = new QMenuBar(this);
    To copy to clipboard, switch view to plain text mode 

    Also my widget is created first inside MainWindow, then the dockwidget then I do:
    Qt Code:
    1. dockwidget->setWidget(wrapper);
    To copy to clipboard, switch view to plain text mode 

    And it doesn't show up. Any clues?

    Quote Originally Posted by jpn View Post
    For a main window, menu bar gets created on demand when QMainWindow::menuBar() is called. If you want a separate menu inside the dock widget:
    Qt Code:
    1. QDockWidget* dockWidget = new QDockWidget(mainWindow);
    2. QWidget* wrapper = new QWidget(dockWidget);
    3. QVBoxLayout* layout = new QVBoxLayout(wrapper);
    4. QMenuBar* menuBar = new QMenuBar(wrapper);
    5. QMenu* dockMenu = menuBar->addMenu("Dock menu");
    6. dockMenu->addAction("blaa");
    7. QTextEdit* textEdit = new QTextEdit(wrapper);
    8. layout->setMenuBar(menuBar);
    9. layout->addWidget(textEdit);
    10. dockWidget->setWidget(wrapper);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Getting QMenuBar of child widget to showup

    Hmm, make sure that all corresponding steps are done than in the example snippet and that at least one menu/action is added to the menu bar.
    J-P Nurmi

  6. #6
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question Re: Getting QMenuBar of child widget to showup

    Quote Originally Posted by jpn View Post
    Hmm, make sure that all corresponding steps are done than in the example snippet and that at least one menu/action is added to the menu bar.
    I know the menu is setup right, because if I pass 0 as the parent it replaces the menu at the top of the MainWindow.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Getting QMenuBar of child widget to showup

    Quote Originally Posted by discostu View Post
    I know the menu is setup right, because if I pass 0 as the parent it replaces the menu at the top of the MainWindow.
    I think you just got me confused. Oh well, here's another example following more closely to what I think you might have. Hopefully this helps.
    Attached Files Attached Files
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    discostu (6th July 2007)

  9. #8
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Talking Re: Getting QMenuBar of child widget to showup

    Quote Originally Posted by jpn View Post
    I think you just got me confused. Oh well, here's another example following more closely to what I think you might have. Hopefully this helps.
    Thanks. The problem seemed to be that the dockwidget was not being passed to the constructor of mywidget, but rather, mywidget was later added to the dockwidget. Doing it this way doest embed mywidget ok, but any menus created in the constructor don't show up.

Similar Threads

  1. Qdialog as a child widget
    By dave in forum Newbie
    Replies: 12
    Last Post: 14th November 2006, 09:43
  2. how to find a child widget?
    By TheRonin in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 10:30
  3. minimize child widget
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2006, 12:02
  4. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  5. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.