Important: Please read the Qt Code of Conduct -

Command line arguments multiple file paths

  • I wrote a Qt application that is able to handle plain text files. I told my operating system (Windows) to use that application to open that file type (extension) if I double-click it in the file explorer. That works fairly well: When I double-click a file with the registered extension it opens my Qt program and passes the file path as a command line parameter.
    From other Windows software I'm used to the fact that when I select multiple files in the file explorer and hit enter, it will again launch the default program for that file extension and pass all selected files as arguments so that at the end there's one instance of that program showing/loading all the selected files.
    However, with my application it's different. When I select multiple files in the file explorer and hit the enter key, it spawns one program instance per selected file. Each program instance gets one file path passed as an argument.

    What exactly is the magic behind all of this? What do I have to do to make windows pass all selected files as command line parameters to one instance of my program?
    I have no idea what causes different behavior here as I expect this to be handled on the operating system level. I don't even know what to google for.
    I am thankful for any kind of advice / hint / remark / solution.

    In case of this is relevant: I'm using the Qt installer framework to install my application on the target computer.

  • Moderators

    AFAIK it's only possible to open one instance per file.
    But there are techniques (DDE for example) to overcome this limitation.

    Or the Qt/x-Platform approach: QtSingleApplication from the QtSolutions repo
    Once you've integrated QtSingleApplication into your application all instances launched will communicate with the first launched instance and should close itself after telling the first instance to open the received file.

  • Lifetime Qt Champion

    Notepad does the same. ( for some ini file)

    I think the apps that dont,
    handles multiple instances them self.
    So if started with a file and already running, give it to other instance.

    (edit: raven-worx was faster :)

  • Thank you for the answers, guys!

    QtSingleApplication appears to be some 3rd party solution, is that correct? It also seems not to be very active/commonly used. Is there another way of doing this using only what the Qt library itself provides?

  • Lifetime Qt Champion


    I think @kshegunov made a nice new version.
    (sorry i remembered wrong, i think it was a demon class)

  • We'll see what he has to add to this once he sees the highlight/notification :)

    At this point I'd like to point out that I WANT to give the user the ability to launch multiple instances of the same program. I want the same behavior as any other Windows software: User can spawn as many instances of the application as he likes, but selecting multiple files in the file explorer and hitting enter will launch only one new instance and pass ALL the selected files as command line parameters to that newly created instance.

    This looks fun too:

  • Lifetime Qt Champion


    The repository might not be highly active but QtSingleApplication seems to be pretty used. There's currently a discussion about having it included in qtbase.

  • Moderators


    Is there another way of doing this using only what the Qt library itself provides?

    There is - duplicating what the mentioned library does - that is opening a local socket and handling the command lines and IPC by hand.

  • Thank you guys!
    I'm going to mark this topic as solved as the question has been answered. Actual implementation will be another problem :p

Log in to reply