0

A few days ago, I asked the same question that I have below but I wrote it out wrong and had an incorrect MRE (minimal reproducible example). I'm reposting (hopefully having fixed the concerns with the other post).

The problem: I can't get my QScrollArea to work. When I create one I (a) don't get a scrollbar and (b) the items inside keep getting smaller the more I add (as opposed to staying the same size)

enter image description here

enter image description here

The MRE (let me know if this is still incorrect for an MRE):

from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QPushButton
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QScrollArea

class MainScreen(QWidget):

    def __init__(self, parent=None):
        # Initialize the class
        super().__init__(parent)

        # Create a box to hold everything in the window
        self.window_layout = QVBoxLayout()

        # Create a label
        title = QLabel("Welcome!")

        # Add the label to the window layout
        self.window_layout.addWidget(title)

        # Create the outer QVBoxLayout
        self.outer_QVBoxLayout = QVBoxLayout()

        # Create a QScrollArea
        self.scroll = QScrollArea()
        self.scroll.setWidgetResizable(True)
        self.scroll.setFixedHeight(200)
        self.scroll.setLayout(self.outer_QVBoxLayout)

        # Add the QAScrollArea to the window
        self.window_layout.addWidget(self.scroll)

        # Enter Button to add the inner_QVBoxLayouts
        button = QPushButton("button")
        button.clicked.connect(self.button_clicked)
        self.window_layout.addWidget(button)

        # Add vertical layout box to the window
        self.setLayout(self.window_layout)

    def button_clicked(self):

        # Make the entry layout
        an_inner_QVBoxLayout = QVBoxLayout()

        # Create a label to put into the layout
        some_text = QLabel("Hello!")
        an_inner_QVBoxLayout.addWidget(some_text)

        self.outer_QVBoxLayout.addLayout(an_inner_QVBoxLayout)


if __name__ == '__main__':
    # Create an instance of QApplication
    app = QApplication(sys.argv)

    # Create login screen
    window = MainScreen()

    # Show the window
    window.show()

    # Exit
    sys.exit(app.exec_())

Thanks

green
  • 77
  • 7
  • Change `self.scroll.setLayout(self.outer_QVBoxLayout)` to `content_widget = QWidget()` `content_widget.setLayout(self.outer_QVBoxLayout)` `self.scroll.setWidget(content_widget)` – eyllanesc Nov 10 '21 at 03:31
  • @eyllanesc firstly, thanks for the help, the scroll bar is showing! Secondly, I was wondering if there is a way to prevent the items inside from resizing. Right now when I put one item in, it takes up the entire space, then when another gets added they take up half each, it isnt until a certain size is hit that the items stop resizing and the scroll bar appears. Is there a way to prevent the initial resizing? – green Nov 10 '21 at 17:24

0 Answers0