Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parent widget not resizing properly if child size is smaller
QtWS25 Last Chance

Parent widget not resizing properly if child size is smaller

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    04CH
    wrote on last edited by 04CH
    #1

    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

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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().

      0 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        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().

        0 Offline
        0 Offline
        04CH
        wrote on last edited by
        #3

        @Chris-Kawa Still not working. Should I change some settings in child layout? Now it's all on default

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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();
          }
          
          0 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            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();
            }
            
            0 Offline
            0 Offline
            04CH
            wrote on last edited by
            #5

            @Chris-Kawa Found mistake in my code. Thank you very much, problem solved.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved