Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. The Extension Example With PyQt4
Forum Updated to NodeBB v4.3 + New Features

The Extension Example With PyQt4

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    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, QtGui

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

    @

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      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)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved