PDA

View Full Version : QMenuBar's height has suddenly changed - bug?



LucaDanieli
27th January 2017, 16:12
I had a QWidget displaying a complex layout containing many widgets.

The code was approximately:


#ifndef Q_OS_MAC
QMenuBar *newMenu = createMenus();
outerLayout->addWidget(newMenu);
#endif

QHBoxLayout * h_layout = new QHBoxLayout;
h_layout->setContentsMargins(0,0,0,0);
h_layout->setSpacing(0);
h_layout->addWidget(newDocumentsDocklet);
h_layout->addWidget(newEditorsDocklet);

outerLayout->addLayout(h_layout);
window->setLayout(outerLayout);

This is the result:

12302

Unfortunately, when I changed my h_layout from QHBoxLayout to QSplitter, the QMenuBar changed its height. I don't understand if this is a bug, as I thought the the QMenuBar had a specific height.

The new code looks like this:


#ifndef Q_OS_MAC
QMenuBar *newMenu = createMenus();
outerLayout->addWidget(newMenu);
#endif

QSplitter * h_layout = new QSplitter;
h_layout->setOrientation(Qt::Horizontal);
h_layout->addWidget(newDocumentsDocklet);
h_layout->addWidget(newEditorsDocklet);

outerLayout->addWidget(h_layout);
window->setLayout(outerLayout);

This is the new rendering:

12303

Is somebody able to help me understand why this has happened? And a way to solve this?

d_stranz
28th January 2017, 00:32
You don't show the code, but I assume "outerLayout" is a QVBoxLayout. What you see is probably due to the layout policy of the VBox. When you add a QWidget and another layout to the VBox, it will expand the layout to fill the space not occupied by the QWidget's preferred size. When you add two QWidget instances to a VBox, it will give them equal height unless you call setStretch() to change the layout ratio.

I'm not really sure why you are manually adding a QMenu and QWidgets to a layout, though. QMainWindow will give a you a menu, and you can set the QMainWindow's centralWidget() to your QSplitter. This will give you exactly the behavior you see in your first case and also behave properly on resize, etc.

LucaDanieli
28th January 2017, 17:35
Thank you d_stranz.
You are actually right: "outerLayout" is a QVBoxLayout.
I already have a QMainWindow, but some users would prefer to be able to duplicate the mainWindow into many (ideally infinite) new windows.

For this reason I add a new menu to every additional new window (It would not be a problem for Macintosh, but this is the only way I have found to have it work in Linux).

I have followed your suggestion, and I have rewritten the code as follows, adding the QSplitter to a Layout to have it stretched automatically, but the result is still the same:


QSplitter * central_body = new QSplitter;
central_body->setOrientation(Qt::Horizontal);
central_body->addWidget(newDocumentsDocklet);
central_body->addWidget(newEditorsDocklet);

QVBoxLayout * central_body_layout = new QVBoxLayout;
central_body_layout->addWidget(central_body);

outerLayout->addLayout(central_body_layout);
window->setLayout(outerLayout);

d_stranz
28th January 2017, 17:47
What you wrote was not what I suggested. I actually didn't suggest anything - I pointed out why there was a difference in behavior between your different implementations. Adding an extra VBox layout inside your existing VBox layout doesn't do anything useful, as you can see.

For a real suggestion, I suggest you remove the extra VBox, and instead change the sizePolicy() of the menu bar to have a vertical size policy of "Fixed".