Resizing widgets in PyQt4

I created the main window using the Qt designer, which has a tabwidget. My problem is that when the window is maximized, the tabwidget remains its original size - thus leaving a gray space on the right.

I would like the main window to always be maximized, so how can I resize the tabwidget to take up 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")) 
+4
source share
1 answer

You need to use QLayout .

You can do this very easily in Designer. Just right-click the form and select Layout and Lay Out Horizontally or Lay Out Vertically - you will need other widgets in the form to see the difference between them. You will see the QLayout added to the Object Inspector, and you can customize its properties as you can using your widgets.

You can also create layouts with code. Here is 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_()) 
+4
source

All Articles