Parent widget not resizing properly if child size is smaller
-
I have a MainWindow with a single child widget (TerminalDisplay), which size depends on font size in it. When I change font size to greater then current, parent resizing works fine, but if I change it to less then current font size my MainWindow leaves blank space and do not fit to the child.
When I changing the font size I call this method:
void TerminalDisplay::updateSize() { QFontMetrics metrics(font()); m_FontHeight = metrics.height(); m_FontWidth = metrics.horizontalAdvance("A"); setFixedSize(m_FontWidth * m_ScreenBuffer.getColumns(), m_FontHeight * m_ScreenBuffer.getLines()); parentWidget()->adjustSize(); }
P.S. On Windows, if I drag MainWindow a little bit it fits to the child. Not working on Linux.
What is the problem here? Thanks
-
The default size constraint of a layout is only considering the minimum size of its contents i.e. it will grow to at least the minimum size required to fit its items, but it will not shrink when there's excess of space. A child that is smaller than the available space will just be positioned inside the layout "cell" according to the alignment setting.
If you want the layout to always shrink/expand to use exactly the amount of space required by the children then change the size constraint of the layout to fixed size:
mainWindow->layout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
This will use the sizeHint() of the children in layout to calculate exact space required and resize the widget automatically. You don't need to call
adjustSize()
. -
The default size constraint of a layout is only considering the minimum size of its contents i.e. it will grow to at least the minimum size required to fit its items, but it will not shrink when there's excess of space. A child that is smaller than the available space will just be positioned inside the layout "cell" according to the alignment setting.
If you want the layout to always shrink/expand to use exactly the amount of space required by the children then change the size constraint of the layout to fixed size:
mainWindow->layout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
This will use the sizeHint() of the children in layout to calculate exact space required and resize the widget automatically. You don't need to call
adjustSize()
.@Chris-Kawa Still not working. Should I change some settings in child layout? Now it's all on default
-
Try this test to see if it works for you. If yes then try to find out what's different in your code.
#include <QApplication> #include <QMainWindow> #include <QPushButton> #include <QLayout> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPushButton* pb = new QPushButton("Click me"); pb->setFixedSize({200,200}); QObject::connect(pb, &QPushButton::clicked, [=]{ if (pb->width() == 200) pb->setFixedSize({400, 400}); else pb->setFixedSize({200, 200}); }); QMainWindow mw; mw.setCentralWidget(pb); mw.layout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize); mw.show(); return a.exec(); }
-
Try this test to see if it works for you. If yes then try to find out what's different in your code.
#include <QApplication> #include <QMainWindow> #include <QPushButton> #include <QLayout> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPushButton* pb = new QPushButton("Click me"); pb->setFixedSize({200,200}); QObject::connect(pb, &QPushButton::clicked, [=]{ if (pb->width() == 200) pb->setFixedSize({400, 400}); else pb->setFixedSize({200, 200}); }); QMainWindow mw; mw.setCentralWidget(pb); mw.layout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize); mw.show(); return a.exec(); }
@Chris-Kawa Found mistake in my code. Thank you very much, problem solved.