简体   繁体   中英

Open multiple files from file managers

I've built a (Linux) GUI application that can be launched from a terminal and accepts an undefined number of files as arguments. The app reads sys.argv and lists the name of these files in a QListWidget.

The code is something like:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QCoreApplication

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # parse command line arguments
        for i in QCoreApplication.argv()[1:]:
            ...

def main():
    app = QApplication(sys.argv)
    ...

What I want to do is to be able to select multiple files from a file manager and open them with my app through the "Open with..." option provided by file managers. How this can be achieved?

With the current code, when I try it only one of the selected files is shown on the QListWidget.

Edit:

It finally seems that it depends to the file manager. I tried with a few file managers and...

  • pcmanfm: It only opens one of the selected files.

  • spacefm: Works properly!

  • dolphin: It opens each file to a different instance of my program. If I select 3 files it will open my app 3 times, one for each file.

  • nautilus: I didn't manage to open any files with it. My program is not listed in the suggested applications and I didn't find any way to do it.

There's not really enough information to give a definite answer, but...

First, have you checked that a print sys.argv at the top of the code looks like you were expecting?

If so, does it work if you change the line...

for i in QCoreApplication.argv()[1:]:

...to...

for i in sys.argv[1:]:

For debugging purposes, you might also like to include the line...

assert QCoreApplication.argv()[1:] == sys.argv[1:]

...just before you start the for-loop.

使用QFileDialog: 文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM