How to create a button for each element in a list and put it in a scroll-area?

前端 未结 2 496
别跟我提以往
别跟我提以往 2021-01-28 12:33

I have a list which gets one element each time user opens a file. I need to create a button with the file\'s name (element from the list), each time this file is appended to a l

2条回答
  •  没有蜡笔的小新
    2021-01-28 13:13

    The problem is that you're not adding a layout to the scrollLayout, you're setting the scrollArea's widget:

    #!/usr/bin/env python
    import os, sys
    from PyQt4 import QtCore, QtGui
    
    
    filenames = []
    
    
    class Window(QtGui.QMainWindow):
    
        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
    
            self.centralwidget = QtGui.QWidget(self)
            self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
            self.scrollArea = QtGui.QScrollArea(self.centralwidget)
            self.scrollArea.setWidgetResizable(True)
    
            self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
            self.scrollArea.setWidget(self.scrollAreaWidgetContents)
            self.verticalLayout.addWidget(self.scrollArea)
            self.setCentralWidget(self.centralwidget)
    
            # create a layout for your scrollarea
            self.formLayout = QtGui.QFormLayout(self.scrollAreaWidgetContents)
            self.addFiles()
    
        def addFiles(self):
            global filenames
            filenames.append("~/files/newFile.txt")
            button = QtGui.QPushButton(os.path.basename(filenames[-1]))
            self.formLayout.addWidget(button)
    

提交回复
热议问题