1

I am having an issue with the QScrollArea and it resizing my widgets. I want all of my layouts and widgets inside of my scroll area to have a fixed height. If I set scroll.setWidgetResizable(True) it will resize everything according to how many widgets were generated. If I set it equal to False then my widgets will not appear. Here is a sample of my code

container = QtWidgets.QWidget()
list_layout = QtWidgets.QVBoxLayout()
container.setLayout(list_layout)
scroll = QtWidgets.QScrollArea()
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
scroll.setWidget(container)

for x in range 10:
    product_container = QtWidgets.QFrame()
    product_container .setFixedHeight(50)
    row = QtWidgets.QHBoxLayout(product_container)
    name = QtWidgets.QLabel(y)
    cbox = QtWidgets.QCheckBox()
    row.addWidget(name)
    row.addWidget(cbox)
    seperator = QtWidgets.QFrame()
    separator.setFrameStyle(QtWidgets.QFrame.HLine | QtWidgets.QFrame.Plain)
    list_layout.addWidget(product_container)
    list_layout.addWidget(separator)

I tried setting my generated containers for the horizontal layout to have a fixed size, (which does work - if I set the background color it shows the box is smaller around my widgets) but the space in between the horizontal layout and the QFrame line is still random.

My end goal is to have a fixed distance in between my horizontal layout and separator

enter image description here

enter image description here

ghost654
  • 161
  • 2
  • 18
  • You could indicate in the images where it is observed that "the space in between the horizontal layout and the QFrame line is still random" – eyllanesc Nov 06 '17 at 17:26
  • added image that shows the background color of the container widget in horizontal layout – ghost654 Nov 06 '17 at 20:03
  • @ghost654. Does my answer solve your problem? I based it on the code in your question, but I'm not sure whether that is the same as the code that produces those screenshots. – ekhumoro Nov 06 '17 at 20:11
  • Yes thank you very much for all your help!! that definitely worked – ghost654 Nov 06 '17 at 22:16

1 Answers1

2

If the rows have a fixed height, all you need to do to is add an expandable space to the end of the container's layout, so that the rows are all pushed upwards:

container = QtWidgets.QWidget()
list_layout = QtWidgets.QVBoxLayout()
container.setLayout(list_layout)
...
for x in range(10):
    ...
    list_layout.addWidget(separator)
list_layout.addStretch()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336