0

I am trying to get files from filedialog and display the name of the file names inside the listview. And also checkboxes should also be created before the filenames inside the listview based on the number of files added. Below is my code that returns only one file with the check box, irrespective of any number of files selected. Help would be appreciated.

def OpenTheFile(self):
    file = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')
    str_file = ','.join(file)
    fileinfo = QFileInfo(str_file)
    filename = QFileInfo.fileName(fileinfo)


    if fileName:
        for files in str_file:
                model = QStandardItemModel()
                item = QStandardItem('%s' % fileName)
                item.setCheckable(True)
                model.appendRow(item)
        self.dlg.DatacheckerlistView2.setModel(model)
        self.dlg.DatacheckerlistView2.show()
harinish
  • 261
  • 3
  • 5
  • 17

1 Answers1

0

It is really unclear what you are doing (there is very little context to your question), but the following code should work. There were several issues with the code in your original question, namely:

  1. You were joining all of the files into a single string and iterating over the string, rather than iterating over the list of filenames.

  2. You were recreating the model each iteration of your loop, effectively deleting any previously added rows.

The code:

files = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')

if files:
    model = QStandardItemModel()
    for file in files:
        item = QStandardItem('%s' % file)
        item.setCheckable(True)
        model.appendRow(item)
    self.dlg.DatacheckerlistView2.setModel(model)
    self.dlg.DatacheckerlistView2.show()
three_pineapples
  • 11,579
  • 5
  • 38
  • 75