Note that there are some explanatory texts on larger screens.

plurals
  1. POStretchy QListBoxWidget
    primarykey
    data
    text
    <h3>Question</h3> <p>How can I create a QListWidget that has a minimum height but stretches to fill all available space even if the layout already has a stretch component?</p> <h3>Background</h3> <p>I'm using PySide to create dynamic User Interfaces. The components of the interface change depending on the context. My goal is to have a vertical layout as such:</p> <p>Controls (grid layout with labels)<br/> Stretch<br/> Buttons (Main operation buttons)<br/> StatusBar<br/></p> <p>The buttons and status bar are just QHBoxLayouts. I'm using a QDialog instead of a QMainWindow since this is often used from within another application such as Maya or Nuke. The dialog is built on the fly and sometimes a QListWidget can be added after the dialog is displayed. I can't predict when to addStretch and when not to.</p> <h3>Trials</h3> <p>I've tried using some of these bits of code to no avail:</p> <pre><code># Set the size Policy. listWidget.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) # Set a minimum Height count = min(listBox.count(),12) bestHeight = count*listBox.sizeHintForRow(0)+5 listBox.setMinimumHeight(bestHeight) </code></pre> <p>setMinimumHeight() as well as setSizeHint(), I've even tried subclassing the QListWidget to control the sizeHint function. * <a href="https://stackoverflow.com/questions/10747752/how-to-set-minimum-height-of-qlistwidgetitem">How to set minimum height of QListWidgetItem?</a> * <a href="https://stackoverflow.com/questions/6337589/qlistwidget-adjust-size-to-content">QListWidget adjust size to content</a></p> <h3>Example</h3> <p>This is a full example. The QListWidget is not stretching to fill the height. If I remove the addStretch() line, the QListWidget performs as I'd like, but I really want this to be dynamic.</p> <pre><code>#!/usr/bin/env python import sys from PySide import QtCore, QtGui class myDialog(QtGui.QDialog): def __init__(self, parent=None): QtGui.QDialog.__init__(self, parent) self.setWindowTitle("Testing 123") # Declare Layouts. self.mainLayout=QtGui.QVBoxLayout() self.gridLayout=QtGui.QGridLayout() self.buttons = QtGui.QHBoxLayout() self.statusBarLayout = QtGui.QHBoxLayout() # Set margins. self.mainLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setContentsMargins(4, 4, 4, 4) self.buttons.setContentsMargins(0, 0, 0, 0) self.statusBarLayout.setContentsMargins(0, 0, 0, 0) # Assemble Layouts. self.mainLayout.addLayout(self.gridLayout) self.mainLayout.addStretch() self.mainLayout.addLayout(self.buttons) self.mainLayout.addLayout(self.statusBarLayout) self.setLayout(self.mainLayout) # Fill in Grid Layout. row = 0 self.gridLayout.addWidget(QtGui.QLabel("ComboBox"), row, 0) combo = QtGui.QComboBox() combo.addItems(['A','B','C','1','2','3']) self.gridLayout.addWidget(combo, row, 1) row += 1 self.gridLayout.addWidget(QtGui.QLabel("List"), row, 0) listWidget = QtGui.QListWidget() listWidget.addItems(['A','B','C','1','2','3']) self.gridLayout.addWidget(listWidget, row, 1) row += 1 self.gridLayout.addWidget(QtGui.QLabel("LineEdit"), row, 0) self.gridLayout.addWidget(QtGui.QLineEdit(), row, 1) row += 1 # Add some buttons. for x in ['One','Two']: self.buttons.addWidget(QtGui.QPushButton(x)) # Add the psuedo status bar. self.statusBar = QtGui.QStatusBar() self.statusBarLayout.addWidget(self.statusBar, 4 ) self.statusBar.showMessage("Hello World") if __name__ == '__main__': app = QtGui.QApplication(sys.argv) dialog = myDialog() sys.exit(dialog.exec_()) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload