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. Adjust mainwindow size when sub widget is hidden
Qt 6.11 is out! See what's new in the release blog

Adjust mainwindow size when sub widget is hidden

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 826 Views 2 Watching
  • 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.
  • A Offline
    A Offline
    akikaede
    wrote on last edited by
    #1

    Hi, there, I'm writing a qt program and trying to resize mainwindow when sub widget is hidden, but there's some difference in Linux and Windows, and I don't know the property way to adjust size in both. Here's my minimum code:

    // MainWindow
        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QWidget *mainWidget = new QWidget(this);
        QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
        QPushButton *hide = new QPushButton(mainWidget);
        subwidget *sub = new subwidget(mainWidget);
        mainlayout->addWidget(hide);
        mainlayout->addWidget(sub);
        connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
        setCentralWidget(mainWidget);
        adjustSize();
        connect(hide, &QPushButton::clicked, this, [&] {
            QTimer::singleShot(0, this, [&] { adjustSize(); });
        });
    }
    
    // Widget
    subwidget::subwidget(QWidget *parent)
        : QWidget{parent}
    {
        QVBoxLayout *vlayout = new QVBoxLayout(this);
        QPushButton *btn1 = new QPushButton();
        btn2 = new QPushButton();
        vlayout->addWidget(btn1);
        vlayout->addWidget(btn2);
    }
    
    void subwidget::onHide()
    {
        btn2->hide();
    }
    

    When I set the timer interval to 1, mainwindow will be the adjusted size when btn2 is hidden in Linux, but it doesn't work in Windows. How can I adjust the mainwindow in windows?

    PS: I have tried to add a signal when subwidget is hidden and connect it to adjustSize like connect(sub, &subwidget::hidden, this, [&] { adjustSize(); });, but it also doesn't work.

    M 1 Reply Last reply
    0
    • A akikaede

      Hi, there, I'm writing a qt program and trying to resize mainwindow when sub widget is hidden, but there's some difference in Linux and Windows, and I don't know the property way to adjust size in both. Here's my minimum code:

      // MainWindow
          MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          QWidget *mainWidget = new QWidget(this);
          QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
          QPushButton *hide = new QPushButton(mainWidget);
          subwidget *sub = new subwidget(mainWidget);
          mainlayout->addWidget(hide);
          mainlayout->addWidget(sub);
          connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
          setCentralWidget(mainWidget);
          adjustSize();
          connect(hide, &QPushButton::clicked, this, [&] {
              QTimer::singleShot(0, this, [&] { adjustSize(); });
          });
      }
      
      // Widget
      subwidget::subwidget(QWidget *parent)
          : QWidget{parent}
      {
          QVBoxLayout *vlayout = new QVBoxLayout(this);
          QPushButton *btn1 = new QPushButton();
          btn2 = new QPushButton();
          vlayout->addWidget(btn1);
          vlayout->addWidget(btn2);
      }
      
      void subwidget::onHide()
      {
          btn2->hide();
      }
      

      When I set the timer interval to 1, mainwindow will be the adjusted size when btn2 is hidden in Linux, but it doesn't work in Windows. How can I adjust the mainwindow in windows?

      PS: I have tried to add a signal when subwidget is hidden and connect it to adjustSize like connect(sub, &subwidget::hidden, this, [&] { adjustSize(); });, but it also doesn't work.

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #3

      @akikaede

      On MacOS, adjustSize has no effect until mainwidow is shown.

      void MainWindow::show()
      {
          QMainWindow::show();
          adjustSize();
      }
      

      [EDIT] I end up with this:

      class subwidget : public QFrame
      {
          public:
      subwidget(QWidget *parent): QFrame{parent}
      {
          setFrameShape(QFrame::Box);
          QVBoxLayout *vlayout = new QVBoxLayout(this);
          QPushButton *btn1 = new QPushButton();
          btn2 = new QPushButton();
          vlayout->addWidget(btn1);
          vlayout->addWidget(btn2);
      }
      
      void onHide()
      {
          btn2->setVisible(!btn2->isVisible());
          adjustSize();
      }
      
          QPushButton* btn2;
      };
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          QWidget *mainWidget = new QWidget(this);
          QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
          QPushButton *hide = new QPushButton(mainWidget);
          hide->setFixedSize(200,200);
          subwidget *sub = new subwidget(mainWidget);
          mainlayout->addWidget(hide);
          mainlayout->addWidget(sub);
          mainlayout->addStretch(0);
          connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
          setCentralWidget(mainWidget);
      
          connect(hide, &QPushButton::clicked, this, [this]
              {
              // update the layout
              this->centralWidget()->layout()->update();
              this->centralWidget()->layout()->activate();
              this->adjustSize();
              });
      }
      
      void MainWindow::show()
      {
          QMainWindow::show();
          adjustSize();
      }
      

      Seems to work...
      Unfortunately, i can't test on Windows.

      A 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        What about adjusting your subwidget object as well ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • A akikaede

          Hi, there, I'm writing a qt program and trying to resize mainwindow when sub widget is hidden, but there's some difference in Linux and Windows, and I don't know the property way to adjust size in both. Here's my minimum code:

          // MainWindow
              MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
          {
              QWidget *mainWidget = new QWidget(this);
              QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
              QPushButton *hide = new QPushButton(mainWidget);
              subwidget *sub = new subwidget(mainWidget);
              mainlayout->addWidget(hide);
              mainlayout->addWidget(sub);
              connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
              setCentralWidget(mainWidget);
              adjustSize();
              connect(hide, &QPushButton::clicked, this, [&] {
                  QTimer::singleShot(0, this, [&] { adjustSize(); });
              });
          }
          
          // Widget
          subwidget::subwidget(QWidget *parent)
              : QWidget{parent}
          {
              QVBoxLayout *vlayout = new QVBoxLayout(this);
              QPushButton *btn1 = new QPushButton();
              btn2 = new QPushButton();
              vlayout->addWidget(btn1);
              vlayout->addWidget(btn2);
          }
          
          void subwidget::onHide()
          {
              btn2->hide();
          }
          

          When I set the timer interval to 1, mainwindow will be the adjusted size when btn2 is hidden in Linux, but it doesn't work in Windows. How can I adjust the mainwindow in windows?

          PS: I have tried to add a signal when subwidget is hidden and connect it to adjustSize like connect(sub, &subwidget::hidden, this, [&] { adjustSize(); });, but it also doesn't work.

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #3

          @akikaede

          On MacOS, adjustSize has no effect until mainwidow is shown.

          void MainWindow::show()
          {
              QMainWindow::show();
              adjustSize();
          }
          

          [EDIT] I end up with this:

          class subwidget : public QFrame
          {
              public:
          subwidget(QWidget *parent): QFrame{parent}
          {
              setFrameShape(QFrame::Box);
              QVBoxLayout *vlayout = new QVBoxLayout(this);
              QPushButton *btn1 = new QPushButton();
              btn2 = new QPushButton();
              vlayout->addWidget(btn1);
              vlayout->addWidget(btn2);
          }
          
          void onHide()
          {
              btn2->setVisible(!btn2->isVisible());
              adjustSize();
          }
          
              QPushButton* btn2;
          };
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
          {
              QWidget *mainWidget = new QWidget(this);
              QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
              QPushButton *hide = new QPushButton(mainWidget);
              hide->setFixedSize(200,200);
              subwidget *sub = new subwidget(mainWidget);
              mainlayout->addWidget(hide);
              mainlayout->addWidget(sub);
              mainlayout->addStretch(0);
              connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
              setCentralWidget(mainWidget);
          
              connect(hide, &QPushButton::clicked, this, [this]
                  {
                  // update the layout
                  this->centralWidget()->layout()->update();
                  this->centralWidget()->layout()->activate();
                  this->adjustSize();
                  });
          }
          
          void MainWindow::show()
          {
              QMainWindow::show();
              adjustSize();
          }
          

          Seems to work...
          Unfortunately, i can't test on Windows.

          A 1 Reply Last reply
          2
          • M mpergand

            @akikaede

            On MacOS, adjustSize has no effect until mainwidow is shown.

            void MainWindow::show()
            {
                QMainWindow::show();
                adjustSize();
            }
            

            [EDIT] I end up with this:

            class subwidget : public QFrame
            {
                public:
            subwidget(QWidget *parent): QFrame{parent}
            {
                setFrameShape(QFrame::Box);
                QVBoxLayout *vlayout = new QVBoxLayout(this);
                QPushButton *btn1 = new QPushButton();
                btn2 = new QPushButton();
                vlayout->addWidget(btn1);
                vlayout->addWidget(btn2);
            }
            
            void onHide()
            {
                btn2->setVisible(!btn2->isVisible());
                adjustSize();
            }
            
                QPushButton* btn2;
            };
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
            {
                QWidget *mainWidget = new QWidget(this);
                QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
                QPushButton *hide = new QPushButton(mainWidget);
                hide->setFixedSize(200,200);
                subwidget *sub = new subwidget(mainWidget);
                mainlayout->addWidget(hide);
                mainlayout->addWidget(sub);
                mainlayout->addStretch(0);
                connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
                setCentralWidget(mainWidget);
            
                connect(hide, &QPushButton::clicked, this, [this]
                    {
                    // update the layout
                    this->centralWidget()->layout()->update();
                    this->centralWidget()->layout()->activate();
                    this->adjustSize();
                    });
            }
            
            void MainWindow::show()
            {
                QMainWindow::show();
                adjustSize();
            }
            

            Seems to work...
            Unfortunately, i can't test on Windows.

            A Offline
            A Offline
            akikaede
            wrote on last edited by
            #4

            @mpergand
            Thank you for your solution! It works both on windows and Linux!

            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