Skip to content Skip to sidebar Skip to footer

Fill A Qlistwidget Using Getopenfilenames

In the example below, I would like to fill my QListWidget with files opening a Qdialog. I don't understand how I can add the files selected in the list. Should I do a new class? Ho

Solution 1:

When self.sources is changed, the elements of the QListWidget will not change. So self.FileList.addItems(self.sources) in setupList() should be removed and instead put in addFiles() so that every time the files are selected in the dialog, the QListWidget's addItems method is called. Then return self.sources in addFiles() would be unnecessary.

Solution 2:

In order to append files to the listwidget, the addFiles method should look like this:

def addFiles(self):
    files = QtGui.QFileDialog.getOpenFileNames(
        self, "Open File", dirpath, "txt Files (*.txt)")
    forstringin files:
        self.FileList.addItem(string)

The source list looks like it might be redundant. If you need to get the full list of files, you can do something like this:

    sources = []
    for row inrange(self.FileList.count()):
        item = self.FileList.item(row)
        # python3
        sources.append(item.text())
        # python2, convert to python strings# sources.append(unicode(item.text()))print(sources)

Post a Comment for "Fill A Qlistwidget Using Getopenfilenames"