The Extension Example With PyQt4
-
what is the effect of leftLayout.addStretch(1) in this code example ? it seems that this program works the same even though leftLayout.addStretch(1) is commented .
@
from PyQt4 import QtCore, QtGuiclass FindDialog(QtGui.QDialog):
def init(self, parent=None):
super(FindDialog, self).init(parent)label = QtGui.QLabel("Find &what:") lineEdit = QtGui.QLineEdit() label.setBuddy(lineEdit) caseCheckBox = QtGui.QCheckBox("Match &case") fromStartCheckBox = QtGui.QCheckBox("Search from &start") fromStartCheckBox.setChecked(True) findButton = QtGui.QPushButton("&Find") findButton.setDefault(True) moreButton = QtGui.QPushButton("&More") moreButton.setCheckable(True) moreButton.setAutoDefault(False) buttonBox = QtGui.QDialogButtonBox(QtCore.Qt.Vertical) buttonBox.addButton(findButton, QtGui.QDialogButtonBox.ActionRole) buttonBox.addButton(moreButton, QtGui.QDialogButtonBox.ActionRole) extension = QtGui.QWidget() wholeWordsCheckBox = QtGui.QCheckBox("&Whole words") backwardCheckBox = QtGui.QCheckBox("Search &backward") searchSelectionCheckBox = QtGui.QCheckBox("Search se&lection") moreButton.toggled.connect(extension.setVisible) extensionLayout = QtGui.QVBoxLayout() extensionLayout.setMargin(0) extensionLayout.addWidget(wholeWordsCheckBox) extensionLayout.addWidget(backwardCheckBox) extensionLayout.addWidget(searchSelectionCheckBox) extension.setLayout(extensionLayout) topLeftLayout = QtGui.QHBoxLayout() topLeftLayout.addWidget(label) topLeftLayout.addWidget(lineEdit) leftLayout = QtGui.QVBoxLayout() leftLayout.addLayout(topLeftLayout) leftLayout.addWidget(caseCheckBox) leftLayout.addWidget(fromStartCheckBox) leftLayout.addStretch(1)############################################### mainLayout = QtGui.QGridLayout() mainLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize) mainLayout.addLayout(leftLayout, 0, 0) mainLayout.addWidget(buttonBox, 0, 1) mainLayout.addWidget(extension, 1, 0, 1, 2) self.setLayout(mainLayout) self.setWindowTitle("Extension") extension.hide()
if name == 'main':
import sys app = QtGui.QApplication(sys.argv) dialog = FindDialog() sys.exit(dialog.exec_())
@
-
In this case it does very little because the of the size of the widgets, the use of a QGridLayout() as the main layout and the fact that the dialog cannot be resized (on Mac anyway). addStretch() puts a QSpacerItem() in the layout with initial size 0 which will then expand to fill any excess space in the layout not required by the widgets (depending on QSizePolicy()'s).
Consider (and run ;o) the following example:
@
from PyQt4.QtCore import *
from PyQt4.QtGui import *class Widget(QWidget):
def init(self, parent=None, **kwargs):
QWidget.init(self, parent, **kwargs)l=QVBoxLayout(self) hl1=QHBoxLayout() hl1.addWidget(QPushButton("Button 1", self)) hl1.addWidget(QPushButton("Button 2", self)) hl1.addStretch() hl2=QHBoxLayout() hl2.addWidget(QPushButton("Button 1", self)) hl2.addWidget(QPushButton("Button 2", self)) l.addLayout(hl1) l.addLayout(hl2)
if name=="main":
from sys import argv, exit
a=QApplication(argv)
w=Widget()
w.show()
w.raise_()
exit(a.exec_())
@When the window is resized (by the user) the buttons in the first layout retain there original size position, whereas the buttons in the second layout expand to fill extra space.
Hope this answers your question ;o)