PDA

View Full Version : QMenuBar doesn't show up properly



Cruz
19th February 2014, 12:33
Hello!

I have a problem with a QMenuBar. It doesn't show up properly. Instead of all the QMenu and QAction inside it, it only shows a ">>" symbol like this:
10053

I can press the symbol and a menu of menus shows up and everything works fine. But this is still a problem. I tried playing around with the size of the widget the QMenuBar is inside, removed all relevant style sheet entries and reduced the contents of the QMenuBar to a single QMenu with one QAction inside. Nothing helps. This happens only on one of two PCs where I tested it (Win XP does not work, Win7 works). The code to produce the menu looks like this:



// Build the menu bar.
QMenuBar* menuBar = new QMenuBar();
QHBoxLayout* menuLayout = new QHBoxLayout(ui.menuWidget);
menuLayout->setMargin(0);
menuLayout->addWidget(menuBar);
menuLayout->addStretch(1);
ui.menuWidget->setLayout(menuLayout);

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

QAction* saveStateAction = fileMenu->addAction(tr("&Save State"));
saveStateAction->setToolTip(tr("Saves the state history."));
saveStateAction->setShortcut(QKeySequence(tr("Ctrl+S")));
connect(saveStateAction, SIGNAL(triggered()), this, SLOT(saveStateHistory()));

// ...and more QMenus and QActions follow.



Any advice?

thanks,
Cruz

anda_skoa
19th February 2014, 13:28
If you want a menu bar, maybe you could use QMainWindow instead? It already has a menu bar and proper layout to handle it.

Cheers,
_

Cruz
19th February 2014, 13:54
I guess I could figure it out. I would want to hide() away the tool bars and the status bar. And I'm using designer to create the gui. Would I just design the central widget and then call ui.setupUi(centralWidget()) in the ctor of the QMainWindow? But even if I do this, the problem described above remains unsolved.

Added after 13 minutes:


and then call ui.setupUi(centralWidget()) in the ctor of the QMainWindow?

No, this doesn't work. I get a segfault right after start. I suppose the central widget doesn't exist yet. So if using a QMainWindow, how do I manage to apply my designed ui to the central widget?

anda_skoa
19th February 2014, 14:39
If you have your widget in designer, you probably have a class that does with it, right?

Just create an instance of that class and set it on the main window using QMainWindow::setCentralWidget()

You can alternatively create the whole main window in designer, then the code stays


ui->setupUi(this);


Cheers,
_

Cruz
19th February 2014, 15:51
Ok yes I could reuse the class I already have for the ui, but then I would separate out half of my gui into a main windows class, where I set up the toolbar and link the central widget, and the other half would remain in my "old" QWidget that currently does it all. I would not like this separation.

The second alternative of designing the whole main windows sort of defeats the purpose of using a QMainWindow, because I would just replace what's already been set up automatically and most likely run into the very same problem I described above.

Before taking the bullet and going with option one, isn't there an explanation + solution for the problem why the menu bar doesn't show up right?

Like this it works perfectly:



MyWindow::MyWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget* cw = new QWidget();
ui.setupUi(cw);
setCentralWidget(cw);

QMenuBar* menuBar = new QMenuBar();
setMenuBar(menuBar);

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

QAction* saveStateAction = fileMenu->addAction(tr("&Save State"));
saveStateAction->setToolTip(tr("Saves the state history."));
saveStateAction->setShortcut(QKeySequence(tr("Ctrl+S")));
connect(saveStateAction, SIGNAL(triggered()), this, SLOT(saveStateHistory()));

[...]
}

anda_skoa
19th February 2014, 19:40
Ok yes I could reuse the class I already have for the ui, but then I would separate out half of my gui into a main windows class, where I set up the toolbar and link the central widget, and the other half would remain in my "old" QWidget that currently does it all. I would not like this separation.

Not necessarily. You could just use your widget and pass in the menu bar or create a menu bar in your widget and set it as the main window's menu bar.



The second alternative of designing the whole main windows sort of defeats the purpose of using a QMainWindow, because I would just replace what's already been set up automatically and most likely run into the very same problem I described above.

Well, I would have just copied the contents of your widget in designer into a QMainWindow based designer form and the code into the main window class.
There would be no "replacing".



Before taking the bullet and going with option one, isn't there an explanation + solution for the problem why the menu bar doesn't show up right?


You could try a couple of things:
1) not adding a stretch
2) setting the menu bar's horizontal size policy to expanding
3) check that your ui.menuWidget is in a layout and that it spans the whole width of the window

Cheers,
_