QTabWidget in PySide Automatically Resize

When using PySide, a QTabWidget is handy. But the size of the QTabWidget is dictated by the largest item, even if it’s not visible.

Let’s assume self.tw is our tab widget. Then add this function:

from PySide.QtCore import *
from PySide.QtGui import *

class MainWindow(QMainWindow):
    def curTabChange(self, index):
        for i in range(self.tw.count()):
            if i == index:
                self.tw.widget(i).setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
            else:
                self.tw.widget(i).setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

    def myOtherFunction(self):
       etc etc etc

And in your initialization associate it with a tab change event:

self.tw = QTabWidget()
        self.tw.currentChanged.connect(self.curTabChange)

Remember to call self.curTabChange(0) probably too once you load tabs.