pyapp-kit / superqt

Missing widgets and components for Qt-python
https://pyapp-kit.github.io/superqt/
BSD 3-Clause "New" or "Revised" License
207 stars 36 forks source link

Extra Space in Closed QCollabsible #214

Closed MosGeo closed 12 months ago

MosGeo commented 1 year ago

Describe the bug I am seeing a lot of empty space below the QCollabsible for some reason. Sometimes, this depends on what is inside the Qcollabsible. Is this by design?

image

To Reproduce

"""Example for QCollapsible"""
from qtpy.QtWidgets import QApplication, QLabel, QPushButton, QWidget, QVBoxLayout, QTableWidget

from superqt import QCollapsible

app = QApplication([])

main_widget = QWidget()
layout = QVBoxLayout()
main_widget.setLayout(layout)

collapsible_bad = QCollapsible("Advanced analysis")
table = QTableWidget(20, 2)
collapsible_bad.addWidget(table)
layout.addWidget(collapsible_bad)

collapsible = QCollapsible("Advanced analysis (not bad)")
collapsible.addWidget(QLabel("This is the inside of the collapsible frame"))
for i in range(3):
    collapsible.addWidget(QPushButton(f"Content button {i + 1}"))
layout.addWidget(collapsible)

layout.addStretch()
main_widget.show()

app.exec_()

Expected behavior I would have expected to the space to be minimum between the two widgets in the example above.

Desktop (please complete the following information):

tlambert03 commented 1 year ago

hey @MosGeo 👋

I think this is because widgets adopt the size policy of their "largest" children if not otherwise set. In your code, if you set collapsible_bad.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Maximum) ... I think it should fix it.

If you try that and determine that it works, and if there don't seem to be any unfortunate side effects of that (depending on what kind of widgets are inside the collapsible) then we might want to set that by default?

MosGeo commented 1 year ago

@tlambert03 Thanks! Yes, with this setting, it is now working as expected. I've tested it with multiple controls and layouts inside. It would make sense to have this as the default as this is the normal "expectation" of any control (to only take the space that it is using).