Can't set fixed/minimal width for QMenuBar CornerWidget
-
Hello everyone!
I want to place window control buttons (close, minimize, etc.) in the menu bar. So I made a QHBoxLayout and added these fixed size buttons to it. Then I made a QWidget and set a layout for that widget and in the end I set that widget as a CornerWidget and set a fixed height for the entire menu bar and a fixed width for the corner widget.
The height is OK, but if set maximum/fixed width for the corner widget, my corner widget looks clipped and there is huge padding on the right.
If I don't do anything with the width, my fixed size buttons are somehow stretched horizontally:
How do I place the buttons in the right corner?
Here is my QMenuBar-subclass constructor body:
setMaximumHeight(22); QWidget* menuWidget = new QWidget(this); QHBoxLayout* menuWidgetLayout = new QHBoxLayout(menuWidget); menuWidget->setLayout(menuWidgetLayout); //menuWidget->setFixedWidth(70); menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonMinimizeWindow")); menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonMaximizeRestoreWindow")); menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonCloseWindow")); setCornerWidget(menuWidget, Qt::TopRightCorner); setContentsMargins(0, 0, 0, 0); menuWidget->setContentsMargins(0, 0, 0, 0); menuWidgetLayout->setContentsMargins(0, 0, 0, 0); cornerWidget()->setContentsMargins(0, 0, 0, 0);
-
As the documentation says the size of the corner widget space is determined based on the widget's sizeHint(). In your case this means the sum of minimum sizes of the buttons plus layout margins. Even if you force the size to be smaller the sizeHint stays the same, so your widget is smaller but is placed in the left side of the space determined by larger sizeHint.
So basically to make the widget smaller, instead of fixing the widget size, make the buttons minimum sizes smaller.
Btw. some unrelated pointers:
QWidget* menuWidget = new QWidget(this);
Don't pass the parent if you're going to use the widget as a corner widget anyway. Tab widget is gonna take ownership of it anyway, so you're paying the unnecessary cost of reparenting here.menuWidget->setLayout(menuWidgetLayout);
This is redundant. You've passed the widget to the layout's constructor, so the layout is already set on the widget. This does nothing here.setContentsMargins(0, 0, 0, 0);
,menuWidget->setContentsMargins(0, 0, 0, 0);
andcornerWidget()->setContentsMargins(0, 0, 0, 0);
Those do nothing. Widgets have a 0 content margin by default. Only layouts don't.
-
@Chris-Kawa, I'm afraid this doesn't work, if I understand you correctly.
In my QPushButton subclass, I have set min and max size and set minimal size policy and also re-implemented sizeHint(), but without any success. I even tried setting the button size fixed with the fixed size policy, but they still stretch. Same for QPushButton.
But thank you very much for your answer and especially for the additional comments. -
It works for me. What if you use a plain QPushButton without text? Is it still this wide? Can you post the code of your button class?
-
@Chris-Kawa, oh, pardon my blindness, it really works. I found that the stylesheet is resetting the width of the button in my case.