0
votes

My program performs a calculation and outputs a few schematics, drawn onto a label (using QPixmap). I show a label on every tab.

When the next calculation is done and the drawings are smaller, I want the size of the tabs to decrease as well. But that does not happen. The tab size stay the same.

I first remove all previous tabs with removeTab() and then create the new tabs. The only thing that is not removed is the QTabWidget itself of course.

When starting with small drawings, the tab size increases with larger drawings. But it doesn't work the other way round.

How can I fix this? The following code does not work:

layout_tabs = new QTabWidget;
layout_tabs->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

I am using QT-4.8.4.

1

1 Answers

-1
votes

Can you (the user) manually resize your window using the mouse to something smaller after switching to the smaller labels?

If so, what's probably happening is that the parent widget (i.e., the window) has resized to accomodate the larger child QTabWidget. When the smaller labels appear after the big labels, QTabWidget will still take up as much space as it is given by its parent unless another widget at the same level in its layout heirarchy is given preference. Because the parent window hasn't gotten smaller, neither will the QTabWidget.

So, try resizing the parent widget (QMainWindow or whatever) after removing the tabs and the tab widget should follow. In Qt, shrinking often has to come from above, whereas expanding can either come from above or below. Something like this might work (untested)...

while(layout_tabs->count() > 0)
{
    QWidget* removedtab = layout_tabs->widget(0);
    layout_tabs->removeTab(0); //removeTab() doesn't delete the widget
    removedtab->deleteLater(); //so you have to delete it yourself
}
layout_tabs->updateGeometry();
mainwindow->resize(mainwindow->minimumSize());

edit Note that QLayoutTab::removeTab() does not delete the tab that you removed, so you will need to delete it yourself somehow, such as in the code snippet I just added, because the removed tab no longer has a parent.