Auto resize the mainwindow to fit the content in the tab widget
-
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:
and if I switch to Tab 2, the window should enlarge as following:
The textEdit widget has a minimum size of 300 x 300.
What I have tried is similar to this answer on stackoverflow. I connect theQTabWidget::currentChanged
toMainWindow::updateSizes
by usingconnect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::updateSizes);
in the constructor ofMainWindow
, and the implementation ofMainWindow::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?
-
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.
-
@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?
-
@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. -
@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.