Виджеты в QVBoxLayout накладываются друг на друга

Я пишу программу на PySide6, создал "шторку", которая меняет свой размер по нажатию на кнопку.

Проблема в том, что, когда Dropdown() находится в свернутом положении, кнопки накладываются друг на друга.

Как можно зафиксировать их положение относительно окна, чтобы при минимальной высоте отображалась только одна верхняя кнопка?

from PySide6.QtCore import Qt, QPropertyAnimation, Property, QEasingCurve
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QVBoxLayout,
    QPushButton,
    QFrame,
)


class Dropdown(QFrame):
    def __init__(self):
        super().__init__()
        self.isMin = False
        self.setFixedSize(300, 135)
        self.setStyleSheet("background-color: white;")

        button = QPushButton("Свернуть/развернуть")
        button.setStyleSheet("background: red;")
        button.setFixedHeight(25)
        button.clicked.connect(self.anim)

        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        layout.addWidget(button)
        for i in range(3):
            btn = QPushButton(f"Button {i}")
            btn.setFixedHeight(25)
            btn.setStyleSheet("background-color: blue;")
            layout.addWidget(btn)
        self.setLayout(layout)

        self.animation = QPropertyAnimation(self, b"Height")
        self.animation.setDuration(300)

    def anim(self):
        if self.isMin:
            self.animation.setStartValue(self.height())
            self.animation.setEndValue(135)
            self.isMin = False
        else:
            self.animation.setStartValue(self.height())
            self.animation.setEndValue(40)
            self.isMin = True
        self.animation.start()

    def setMinHeight(self, height):
        self.setFixedHeight(height)

    def getMinHeight(self):
        return self.height()

    Height = Property(int, getMinHeight, setMinHeight)


if __name__ == "__main__":
    app = QApplication()
    w = QMainWindow()
    w.setCentralWidget(Dropdown())
    w.show()
    app.exec()

Ответы (1 шт):

Автор решения: S. Nick

Виджеты отрисовываются в порядки их поступления.
Попробуйте заменить

layout.addWidget(button)

на

layout.insertWidget(0, button)

Поменяйте на свои импорты и попробуйте:

import sys
''' 
from PySide6.QtCore import (Qt, QPropertyAnimation,
    Property as pyqtProperty,                               # +++
    QEasingCurve)
from PySide6.QtWidgets import (QApplication,
    QMainWindow,QVBoxLayout, QPushButton, QFrame,)
'''               
from PyQt5.Qt import *


class Dropdown(QFrame):
    def __init__(self):
        super().__init__()
        
        self.isMin = False
        self.setFixedSize(300, 135)
        self.setStyleSheet("background-color: white;")

        button = QPushButton("Свернуть/развернуть")
        button.setStyleSheet("background: red;")
        button.setFixedHeight(25)
        button.clicked.connect(self.anim)

        layout = QVBoxLayout(self)                            # +++
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
# !!!   layout.addWidget(button)                              # --- !!!
        
        for i in range(3):
            btn = QPushButton(f"Button {i}")
            btn.setFixedHeight(25)
            btn.setStyleSheet("background-color: blue;")
            layout.addWidget(btn)
# !!! +++            
        layout.insertWidget(0, button)                        # !!! +++
            
#        self.setLayout(layout)                               # ---

        self.animation = QPropertyAnimation(self, b"Height")
        self.animation.setDuration(300)

    def anim(self):
        if self.isMin:
            self.animation.setStartValue(self.height())
            self.animation.setEndValue(135)
            self.isMin = False
        else:
            self.animation.setStartValue(self.height())
            self.animation.setEndValue(35)                    # 40
            self.isMin = True
        self.animation.start()

    def setMinHeight(self, height):
        self.setFixedHeight(height)

    def getMinHeight(self):
        return self.height()

#    Height = Property(int, getMinHeight, setMinHeight)     # PySide6
    Height = pyqtProperty(int, getMinHeight, setMinHeight)  # +++


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QMainWindow()
    w.setCentralWidget(Dropdown())
    w.show()
    sys.exit(app.exec())

введите сюда описание изображения

введите сюда описание изображения

→ Ссылка