我试图在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_())
一个QScrollArea只能包含一个小部件,那么如何在一个QScrollArea中放置几个小部件呢?在Qt中,QWidget也可以用作容器,因此在这种情况下,您必须创建一个布局稳定的content\u小部件,并在该布局中放置小部件。另一方面,在for循环中,通常不建议创建属性。你知道吗
相关问题 更多 >
编程相关推荐