Skip to content Skip to sidebar Skip to footer

Resizing Widgets In PyQt4

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

Solution 1:

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_())

Post a Comment for "Resizing Widgets In PyQt4"