T3DS
T3DS

Reputation: 31

How to add multiple layouts in a QScrollArea

I am trying to add a bunch of widgets to a QScrollArea in PyQt but i can't seem to get it to work.

What I want to do is to get some information from a list and add it to the QScrollArea but it's only displaying the last item in the list. I'm still a novice at python and PyQt so I apologise if the question is stupid,

SOURCE:

class Window(QFrame):
    def __init__(self):
        super(Window,self).__init__()
        self.setStyle(QStyleFactory.create('Cleanlooks'))
        self.setGeometry(300, 300, 600, 600)
        self.setWindowTitle("Reddit")
        self.show()
        self.mainWindow()

    def mainWindow(self):
        self.submissionLayout = QVBoxLayout()
        self.scrollArea = QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.submissionLayout.addWidget(self.scrollArea)
        self.setLayout(self.submissionLayout)

        #to handle all the api calls using praw
        self.x = RedditApi()
        self.printSubmissions()

    def printSubmissions(self):
        #Gets the list of all submission titles to be displayed
        #TO DO: Get and add other things like points and comments

        self.submissions = self.x.showSubmissions()

        for submission in self.submissions:
            self.subcard = QVBoxLayout()
            self.subcard.addStretch()
            self.subtitle=QLabel()
            print(submission)
            self.subtitle.setText(submission)
            self.subcard.addWidget(self.subtitle)
            self.card = QWidget()
            self.card.setLayout(self.subcard)
            self.scrollArea.setWidget(self.card)


if __name__ == '__main__':
app = QApplication([])
window = Window()
sys.exit(app.exec_())

Upvotes: 1

Views: 1336

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

A QScrollArea can only contain a single widget, so how can I place several in a QScrollArea? In Qt a QWidget can also be used as a container so in this case you must create a content_widget where a layout is stable, and in that layout place the widgets. On the other hand in a for loop in general it is not recommended to create attributes.

from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QFrame):
    def __init__(self):
        super(Window,self).__init__()
        self.setStyle(QtWidgets.QStyleFactory.create('Cleanlooks'))
        self.setGeometry(300, 300, 600, 600)
        self.setWindowTitle("Reddit")
        self.mainWindow()
        self.show()

    def mainWindow(self):
        submissionLayout = QtWidgets.QVBoxLayout(self)
        scrollArea = QtWidgets.QScrollArea(widgetResizable=True)
        submissionLayout.addWidget(scrollArea)
        content_widget = QtWidgets.QWidget()
        scrollArea.setWidget(content_widget)
        self.scroll_layout = QtWidgets.QVBoxLayout(content_widget)
        #to handle all the api calls using praw
        self.x = RedditApi()
        self.printSubmissions()

    def printSubmissions(self):
        #Gets the list of all submission titles to be displayed
        #TO DO: Get and add other things like points and comments
        self.submissions = self.x.showSubmissions()

        for submission in self.submissions:
            card = QtWidgets.QWidget()
            subtitle = QtWidgets.QLabel(submission)
            subcard = QtWidgets.QVBoxLayout(card)
            subcard.addStretch()
            subcard.addWidget(subtitle)
            self.scroll_layout.addWidget(card)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication([])
    w = Window()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions