Search code examples
pythonqtpyqtqpushbuttonqscrollarea

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


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 list, and put this button into a scroll-area.

The problem is that I always have only one button, that just changes its name:

filenames = []
def addfiles():
    fileName = QtGui.QFileDialog.getOpenFileName()
    fileDirectory = unicode(fileName)  
    global filenames
    filenames.append(fileDirectory)
    button = QtGui.QPushButton(os.path.basename(fileDirectory))
    window.scrollArea.setWidget(button)

I know that the problem is that I add the same object (button) to the scroll-area, but I don't know how to fix it.


Solution

  • The Problem is not that you add the same button, but that you sort of replace the Widget in the scrollArea.

    A better way would be to create a QHBoxLayout and than add the buttons to the layout.

    filenames = []
    lay = QtGui.QHboxLayout()
    window.scrollArea.setLayout(lay)
    def addfiles():
        fileName= QtGui.QFileDialog.getOpenFileName()
        fileDirectory = unicode(fileName)  
        global filenames
        filenames.append(fileDirectory)
        button = QtGui.QPushButton(os.path.basename(fileDirectory))
        lay.addWidget(button)
    

    In a sort of that way it should work. Here is a small working example:

    from PyQt4 import QtGui
    import sys
    
    filenames = []
    
    class TestGui(QtGui.QWidget):
        """ A Fast test gui show how to create buttons in a ScrollArea"""
        def __init__(self):
            super(TestGui, self).__init__()
            self.lay = QtGui.QHBoxLayout()
            self.sA = QtGui.QScrollArea()
            self.sA_lay = QtGui.QVBoxLayout()
            self.sA.setLayout(self.sA_lay)
            self.closeGui = QtGui.QPushButton("Close")
            self.add_file_button = QtGui.QPushButton("Add File")
            self.lay.addWidget(self.closeGui)
            self.lay.addWidget(self.add_file_button)
            self.lay.addWidget(self.sA)
            self.setLayout(self.lay)
            self.connect_()
            self.show()
    
        def connect_(self):
            self.add_file_button.clicked.connect(self.__add_file_to_list)
            self.closeGui.clicked.connect(self.close)
            return
    
        def __add_file_to_list(self):
            fname = QtGui.QFileDialog.getOpenFileName()
            global filenames
            filenames.append(fname)
            button = QtGui.QPushButton(fname)
            self.sA_lay.addWidget(button)
            return
    
    
    if __name__ == '__main__':
         app = QtGui.QApplication(sys.argv)
         tg = TestGui()
         sys.exit(app.exec_())