Resizing widgets in PyQt4

pyqt4, python, resize, widget

Solution

You'll need to use a `QLayout` of some kind.

You can do this very easily in Designer. Just right click the form and choose `Layout` and either `Lay Out Horizontally` or `Lay Out Vertically` - you'll need other widgets on the form to see the difference between the two. You'll see the `QLayout` added in the Object Inspector and you'll be able to adjust its properties like you can with your widgets.

You can also create layouts with code. Here's a working example:

import sys
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QApplication, QWidget, QTabWidget, QHBoxLayout

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        # Create the layout.
        self.h_layout = QHBoxLayout()

        # Create the QTabWidget.
        self.tabWidget = QTabWidget()
        self.tabWidget.setEnabled(True)
        self.tabWidget.setGeometry(QRect(20, 40, 601, 501))
        self.tabWidget.setTabPosition(QTabWidget.North)
        self.tabWidget.setObjectName('tabWidget')

        # Add the QTabWidget to the created layout and set the 
        # layout on the QWidget.
        self.h_layout.addWidget(self.tabWidget)
        self.setLayout(self.h_layout)


if __name__ == '__main__':
  app = QApplication(sys.argv)

  widget = Widget()
  widget.show()

  sys.exit(app.exec_())

Problem

I have created a main window using Qt designer that has a tabwidget in it. My problem is that when the window is maximized, the tabwidget remains its original size - thus leaving a lot of grey space to its right. I would like for the main window to always be maximized, so how can I resize the tabwidget to occupy more space? What can I add to the following code to achieve this? ``` self.tabWidget = QtGui.QTabWidget(self.centralwidget) self.tabWidget.setEnabled(True) self.tabWidget.setGeometry(QtCore.QRect(20, 40, 601, 501)) self.tabWidget.setTabPosition(QtGui.QTabWidget.North) self.tabWidget.setObjectName(_fromUtf8("tabWidget")) ```

Original source