0

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.

puzzle
  • 6,071
  • 1
  • 25
  • 30

1 Answers1

-1

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.

Phlucious
  • 3,704
  • 28
  • 61
  • Thanks for your answer! Yes I can resize the window using the mouse :-) Why didn't I think about that. Ok I will try the updategeometry() and will let you know if it was successfull. – Kees Bergwerf Feb 08 '13 at 20:16
  • One problem was that I put my qtabwidget into a qdockwidget. WHen I just do layout_tabs = new QTabWidget; all works well. Except that the tabs don't close when my main window shuts down. – Kees Bergwerf Feb 08 '13 at 21:37
  • Make sure that every widget that you `new` has a `QObject` parent, either by passing one to its constructor or by placing the widget inside another widget via a layout. QObjects, including QWidgets and their descendents, will delete any children in their destructors. Also see my new comment about [QTabWidget::removeTab()](http://qt-project.org/doc/qt-4.8/qtabwidget.html#removeTab). – Phlucious Feb 09 '13 at 00:16