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. Auto resize the mainwindow to fit the content in the tab widget

Auto resize the mainwindow to fit the content in the tab widget

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.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.
  • H Offline
    H Offline
    hanatok
    wrote on last edited by
    #1

    Hello,
    I am working on a multi-tab Qt application and want to shrink or enlarge the main window to fit into the minimum size of the widgets in the active tab. More specifically, the main window shrinks like the following figure when I switch to Tab 1:
    Tab 1
    and if I switch to Tab 2, the window should enlarge as following:
    Tab 2
    The textEdit widget has a minimum size of 300 x 300.
    What I have tried is similar to this answer on stackoverflow. I connect the QTabWidget::currentChanged to MainWindow::updateSizes by using connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::updateSizes); in the constructor of MainWindow, and the implementation of MainWindow::updateSizes is:

    void MainWindow::updateSizes(int index)
    {
      for(int i=0;i<ui->tabWidget->count();i++)
          if(i!=index)
              ui->tabWidget->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    
      ui->tabWidget->widget(index)->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
      ui->tabWidget->widget(index)->resize(ui->tabWidget->widget(index)->minimumSizeHint());
      ui->tabWidget->widget(index)->adjustSize();
      resize(minimumSizeHint());
      adjustSize();
    }
    

    The problem is that the main window is correctly resized when I switch to Tab 2 (see the gif below), but when I switch back to Tab 1, it does not shrink. Any ideas?
    animation

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see a reason why it should shrink. The tab widget now knows that it needs that size so it does not shrink.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      H 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        I don't see a reason why it should shrink. The tab widget now knows that it needs that size so it does not shrink.

        H Offline
        H Offline
        hanatok
        wrote on last edited by
        #3

        @Christian-Ehrlicher I think Tab 1 has a minimum size, which should be different from the minimum size of Tab 2. When switching back to Tab 1, I want the main window to shrink to fit the minimum size of Tab 1. Isn't it possible?

        Christian EhrlicherC 1 Reply Last reply
        0
        • H hanatok

          Hello,
          I am working on a multi-tab Qt application and want to shrink or enlarge the main window to fit into the minimum size of the widgets in the active tab. More specifically, the main window shrinks like the following figure when I switch to Tab 1:
          Tab 1
          and if I switch to Tab 2, the window should enlarge as following:
          Tab 2
          The textEdit widget has a minimum size of 300 x 300.
          What I have tried is similar to this answer on stackoverflow. I connect the QTabWidget::currentChanged to MainWindow::updateSizes by using connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::updateSizes); in the constructor of MainWindow, and the implementation of MainWindow::updateSizes is:

          void MainWindow::updateSizes(int index)
          {
            for(int i=0;i<ui->tabWidget->count();i++)
                if(i!=index)
                    ui->tabWidget->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
          
            ui->tabWidget->widget(index)->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
            ui->tabWidget->widget(index)->resize(ui->tabWidget->widget(index)->minimumSizeHint());
            ui->tabWidget->widget(index)->adjustSize();
            resize(minimumSizeHint());
            adjustSize();
          }
          

          The problem is that the main window is correctly resized when I switch to Tab 2 (see the gif below), but when I switch back to Tab 1, it does not shrink. Any ideas?
          animation

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @hanatok
          You probably won't thank me for this, and of course you can do as your please, but can you name an application whose actual main window size keeps growing & shrinking as the user interacts with it in this fashion? IMHO very disconcerting for user.

          1 Reply Last reply
          0
          • H hanatok

            @Christian-Ehrlicher I think Tab 1 has a minimum size, which should be different from the minimum size of Tab 2. When switching back to Tab 1, I want the main window to shrink to fit the minimum size of Tab 1. Isn't it possible?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @hanatok said in Auto resize the mainwindow to fit the content in the tab widget:

            Isn't it possible?

            It is, but it would make me as a user go crazy.
            You have to set the minimum size for all pages, since this is the size the tabwidget uses.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hanatok
              wrote on last edited by
              #6

              @Christian-Ehrlicher @JonB
              Thanks for your opinion. I will avoid such things and find a better solution.

              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