PDA

View Full Version : QlistWidget vertical resize to minimum required size



renevolution
29th April 2015, 16:49
Hey there,

i am actually having big troubles to get a QListWidget to work, so that it resizes (expands) vertically but to a minimum required size.
It should adjust if the QListWidget expands horizontally. So, in general it should just request for the space it's childs request. Did try to find a solution for the whole day and the closest i was able to get was this:



import sys
from PySide import QtGui, QtCore

if __name__ == '__main__':
app = QtGui.QApplication([])

window = QtGui.QWidget()
layout = QtGui.QVBoxLayout(window)



list = QtGui.QListWidget()

sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(list.sizePolicy().has HeightForWidth())

list.setSizePolicy(sizePolicy)
list.setFocusPolicy(QtCore.Qt.NoFocus)
list.setFrameShape(QtGui.QFrame.NoFrame)
list.setLineWidth(0)
list.setAutoScroll(False)
list.setProperty("showDropIndicator", False)
list.setDragDropMode(QtGui.QAbstractItemView.NoDra gDrop)
list.setSelectionMode(QtGui.QAbstractItemView.NoSe lection)
list.setSelectionBehavior(QtGui.QAbstractItemView. SelectRows)
list.setMovement(QtGui.QListView.Free)
list.setFlow(QtGui.QListView.LeftToRight)
list.setProperty("isWrapping", True)
list.setResizeMode(QtGui.QListView.Adjust)
list.setLayoutMode(QtGui.QListView.Batched)
list.setViewMode(QtGui.QListView.ListMode)
list.setSelectionRectVisible(True)
list.setHorizontalScrollBarPolicy(QtCore.Qt.Scroll BarAlwaysOff)
list.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBa rAlwaysOff)

for i in range(2):
item = QtGui.QListWidgetItem()
widget = QtGui.QWidget()

widget_layout = QtGui.QHBoxLayout()
edit = QtGui.QLineEdit()
label = QtGui.QLabel("I love PyQt!")
widget_layout.addWidget(label)
widget_layout.addWidget(edit)
widget_layout.addStretch()
widget_layout.setSizeConstraint(QtGui.QLayout.SetF ixedSize)
widget.setLayout(widget_layout)
item.setSizeHint(widget.sizeHint())
list.addItem(item)
list.setItemWidget(item, widget)


list.setFixedSize(list.sizeHintForColumn(0) + 2 * list.frameWidth(), list.sizeHintForRow(0) * list.count() + 2 * list.frameWidth())

layout.addWidget(list)
window.show()

sys.exit(app.exec_())


Hope someone can help me here ...

Thanks,
René

renevolution
30th April 2015, 09:20
Hey again,

i am still stuck with this. I also tried subclassing the QListWidget and use a custom sizeHint() function.
I really wonder that it works for the horizontal part. I have set the QListWidget to IconMode and Expanding for horizontal.
It now flows correct with showing as many items in a row as the width allows. but this does not work for the vertical handling.
This always remains the same.

I really need some help here ...

Thanks,
René