PDA

View Full Version : Getting QMenuBar of child widget to showup



discostu
5th July 2007, 18:22
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)

MyWidget *self = const_cast<MyWidget*>(this);
mMenuBar = new QMenuBar(self);

And then:


QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMenuBar(mMenuBar);
mainLayout->addWidget(mTextEdit);

But it doesn't show up.

guilugi
5th July 2007, 19:03
Your menubar is related to your QMainWindow widget, right ?

What if you add it like this :


mainwindow->setMenuBar(mMenuBar);

Or maybe, I misunderstood your issue...

jpn
5th July 2007, 19:12
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:


QDockWidget* dockWidget = new QDockWidget(mainWindow);
QWidget* wrapper = new QWidget(dockWidget);
QVBoxLayout* layout = new QVBoxLayout(wrapper);
QMenuBar* menuBar = new QMenuBar(wrapper);
QMenu* dockMenu = menuBar->addMenu("Dock menu");
dockMenu->addAction("blaa");
QTextEdit* textEdit = new QTextEdit(wrapper);
layout->setMenuBar(menuBar);
layout->addWidget(textEdit);
dockWidget->setWidget(wrapper);

discostu
5th July 2007, 20:54
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:


QVBoxLayout* layout = new QVBoxLayout(this);
QMenuBar* menuBar = new QMenuBar(this);

Also my widget is created first inside MainWindow, then the dockwidget then I do:

dockwidget->setWidget(wrapper);

And it doesn't show up. Any clues?


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:


QDockWidget* dockWidget = new QDockWidget(mainWindow);
QWidget* wrapper = new QWidget(dockWidget);
QVBoxLayout* layout = new QVBoxLayout(wrapper);
QMenuBar* menuBar = new QMenuBar(wrapper);
QMenu* dockMenu = menuBar->addMenu("Dock menu");
dockMenu->addAction("blaa");
QTextEdit* textEdit = new QTextEdit(wrapper);
layout->setMenuBar(menuBar);
layout->addWidget(textEdit);
dockWidget->setWidget(wrapper);

jpn
5th July 2007, 21:28
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.

discostu
5th July 2007, 23:05
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.

jpn
6th July 2007, 06:40
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.

discostu
6th July 2007, 18:56
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.