Layout inside QScrollArea inside Layout [duplicate]

3 weeks ago 36
ARTICLE AD BOX

I want to make a scrollable QVBoxLayout inside another QVBoxLayout. My plan is to have a hierarchy like this:

QMainWindow

QWidget

QVBoxLayout

QWidget with QScrollArea.setWidget() called on it

QVBoxLayout

Here is a minimal reproducible example:

import sys from PySide6.QtCore import Qt from PySide6.QtWidgets import ( QApplication, QLabel, QMainWindow, QScrollArea, QVBoxLayout, QWidget, ) class MainWindow(QMainWindow): def __init__(self) -> None: super().__init__() self.widget = QWidget(self) self.setCentralWidget(self.widget) self.vbox_outer = QVBoxLayout() self.widget.setLayout(self.vbox_outer) self.vbox_outer.addWidget(QLabel("TOP")) self.scroll_area = QScrollArea(self.widget) self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.scroll_area.setWidgetResizable(True) self.vertical_layout_widget = QWidget(self.scroll_area) self.scroll_area.setWidget(self.vertical_layout_widget) self.vbox_inner = QVBoxLayout() self.vertical_layout_widget.setLayout(self.vbox_inner) self.vbox_outer.addWidget(self.vertical_layout_widget) for _i in range(1, 50): obj = QLabel("TextLabel", self.vertical_layout_widget) self.vbox_inner.addWidget(obj) self.vbox_outer.addWidget(QLabel("BOTTOM")) self.setGeometry(0, 0, 1000, 500) self.show() app = QApplication(sys.argv) main = MainWindow() sys.exit(app.exec())

I can get close to what I want if the QScrollArea does not contain a QVBoxLayout but instead a string with many lines, or if the central widget is the QScrollArea itself. I started with this example but for that the scrollarea was the central widget and there was no UI outside the scrollarea. These questions are similar but different enough that I can't get things working: 1 2

Read Entire Article