-2

i have code which can select multiple files and print their names. But i found that they are not in order in which i am selecting them. say for example if i have file name as file1,file2,file3and if i first select file2 then file3 then file1 while printing it is showing file1,file2,file3 in this order. the code i have written is like this

    global desktop1
    global filesnames1
    file_name = QtGui.QFileDialog()
    file = file_name.getOpenFileNames(self, "Select the file to add", desktop1,
                                                      "source File (*.MOV;*.MP4)")

    filesnames1=list(file)
    for i in filesnames1:
       print i

please help how to achieve what i want.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • I found the solution instead of declaring the variable as **list** we need to declare it as **set** coz list always sort the element where as set doesn't. – Ramakanta Chandra Aug 13 '16 at 10:06
  • This is completely wrong. A set has no defined order. If you convert the list of files to a set, it will be pure coincidence if they appear in the order you wanted when you iterate over them. Try making a larger selection of files: it is almost certain they will appear in the "wrong" order when you iterate over them. – ekhumoro Aug 13 '16 at 15:20
  • @ekhumoro yes u r right. Sorry for coming to d decision without checking it properly. – Ramakanta Chandra Aug 14 '16 at 07:52

2 Answers2

3

By default, the static functions (like getOpenFileNames) will open a native file-dialog whenever possible. For native file-dialogs, Qt has no control over the order of the returned files, so there's nothing you can do to change it.

However, it seems that the built-in Qt file-dialog does return the files in the order they were selected. So if you don't need a native file-dialog, you can simply do this:

files = QFileDialog.getOpenFileNames(
    self, "Select the file to add", desktop1, "source File (*.mov *.mp4)",
    options=QFileDialog.DontUseNativeDialog
    )
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • when trying with this code the .MOV or .MP4 files it is not showing. in fact it is not showing any files when trying with other file types – Ramakanta Chandra Aug 16 '16 at 07:51
  • @RamakantaChandra. Well, I just copied your code, which I assumed was working. Anyway, I've edited my answer, which works fine for me on linux. – ekhumoro Aug 16 '16 at 23:13
0

I don't think there is a method for that but you can write a function to get the selection and compare it to the previous one and connect that function to the activation of your QFileDialog :

global desktop1
global previous_selection
global filesnames1
QtGui.QFileDialog.currentChanged(get_selection)
file_name = QtGui.QFileDialog()


def get_selection(previous_selection):
    file = file_name.getOpenFileNames(self, "Select the file to add", desktop1,
                                                  "source File (*.MOV;*.MP4)")
    selected_files=list(file)
    if len(selected_files) > 1:
        current_file = selected_files-filenames1
        filenames1.append(current_file)
    else:
        filenames1=list()
        filenames1.append(selected_files[0])

I didn't try that code, but I think something like that would do the trick.

Martin
  • 105
  • 1
  • 10
  • I didn't really expect it to work just like that (since I didn't try it), I just wanted to show you the idea of something which could work. I'll try to come with something better once I get access to a python editor if no one answers first. – Martin Aug 13 '16 at 18:12