【问题标题】:How to add multiple layouts in a QScrollArea如何在 QScrollArea 中添加多个布局
【发布时间】:2019-03-13 18:58:23
【问题描述】:

我正在尝试向 PyQt 中的 QScrollArea 添加一堆小部件,但我似乎无法让它工作。

我想要做的是从列表中获取一些信息并将其添加到 QScrollArea 但它只显示列表中的最后一项。我仍然是 python 和 PyQt 的新手,所以如果问题很愚蠢,我深表歉意,

来源:

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_())

【问题讨论】:

    标签: python pyqt5 qscrollarea


    【解决方案1】:

    一个 QScrollArea 只能包含一个小部件,那么如何在 QScrollArea 中放置多个小部件?在 Qt 中,QWidget 也可以用作容器,因此在这种情况下,您必须创建一个布局稳定的 content_widget,并在该布局中放置小部件。另一方面,在 for 循环中,一般不建议创建属性。

    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_())
    

    【讨论】:

    • 那么你推荐什么而不是将它们添加到 for 循环中?也许创建一个单独的函数并在 for 循环中传递它?
    • @T3DS 我想你没听懂我的意思,我指出你不应该创建与变量不同的属性,一个属性可以通过self在整个类中访问,但一个变量不是,在我的解决方案中,我将self.subcard 更改为subcard
    • 对不起,我在想一些完全不同的东西,但我现在明白你的意思了。谢谢。
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2018-01-09
    • 1970-01-01
    相关资源
    最近更新 更多