Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to find the path of the file that launches the Qt application from explorer
Forum Updated to NodeBB v4.3 + New Features

How to find the path of the file that launches the Qt application from explorer

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 7.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by sierdzio
    #2
    const QStringList args(QCoreApplication::instance()->arguments());
    qDebug() << "File path:" << args.at(1);
    

    That should work.

    Edit: Fixed at(0) to at(1)

    (Z(:^

    JonBJ 1 Reply Last reply
    4
    • sierdzioS sierdzio
      const QStringList args(QCoreApplication::instance()->arguments());
      qDebug() << "File path:" << args.at(1);
      

      That should work.

      Edit: Fixed at(0) to at(1)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @sierdzio , @QtVik
      Using QCoreApplication::instance()->arguments()[0] is a most unreliable way to obtain a program's path. It may work reliably from Windows Explorer, I don't know, but it can be set to any string the launcher desires and/or can contain the right name but no path.

      I would suggest you should use QCoreApplication::applicationFilePath(), which I read from the docs as making an attempt to "get it right", and hopefully will be implemented using the correct Windows native OS call. You may failover to arguments()[0] if you wish.

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #4

        @JNBarchan the OP is not asking for application file path. They are asking for path to file which has been double clicked on in the explorer.

        (Z(:^

        JonBJ 1 Reply Last reply
        0
        • sierdzioS sierdzio

          @JNBarchan the OP is not asking for application file path. They are asking for path to file which has been double clicked on in the explorer.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #5

          @sierdzio
          Oh, maybe you interpret the question better than I, then!

          But then I don't understand your answer, which has 2 upvotes? From the docs (http://doc.qt.io/qt-5/qcoreapplication.html#arguments):

          Usually arguments().at(0) is the program name,

          Are you saying that under Windows argument #0 is the "name of the program", and if run from Explorer it will be the full path to the .dat file, not the name/path of his Qt application? So an application has to know whether it was invoked from Explorer or not in order to know what its argv[0] will represent?? I don't run Qt under Windows, but this does not sound right to me.

          mrjjM 1 Reply Last reply
          2
          • JonBJ JonB

            @sierdzio
            Oh, maybe you interpret the question better than I, then!

            But then I don't understand your answer, which has 2 upvotes? From the docs (http://doc.qt.io/qt-5/qcoreapplication.html#arguments):

            Usually arguments().at(0) is the program name,

            Are you saying that under Windows argument #0 is the "name of the program", and if run from Explorer it will be the full path to the .dat file, not the name/path of his Qt application? So an application has to know whether it was invoked from Explorer or not in order to know what its argv[0] will represent?? I don't run Qt under Windows, but this does not sound right to me.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #6

            @JNBarchan

            Docs do mention it can be different

            "...GetCommandLine(). As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started."

            So maybe starting from explorer is one of those cases :)

            Update:
            Nope, assign .rip to my own exe shows appname in (0) and param in (1)
            alt text

            1 Reply Last reply
            1
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              Yes, QCoreApplication::instance()->arguments()[0] will always refer to the "program", but may be just the name instead of a full path, and shouldn't be relied on anyway, as I wrote earlier.

              So [0] is never to be used. I suspect @sierdzio was intending arguments()[1] maybe.

              However, while this may work for the OP's situation, other readers should be aware of the following. You can never be sure "which filename was clicked on in Explorer to launch application" in Windows (I suspect Linux desktops too).

              When you make a "File Association" under Windows, it creates an entry in the Registry. IIRC, this is/can be a "command-line", in which the file being double-clicked on is referenced by something like %1. Although the default may be that this is the first argument (arguments()[1]), plenty of programs have preceding command-line options in their command in the registry, like -a -b "%1" (or a /print %1 for printing, for example). In this case argument 1 would be -a, not what the OP wants.

              Only your program knows where it expects Explorer to pass the file path of the file clicked on the command-line, depending on option processing.

              If you want to guess, the last rather then the first argument is probably advisable, e.g. arguments()[arguments().length - 1]. But you can't be sure.

              sierdzioS 1 Reply Last reply
              4
              • JonBJ JonB

                Yes, QCoreApplication::instance()->arguments()[0] will always refer to the "program", but may be just the name instead of a full path, and shouldn't be relied on anyway, as I wrote earlier.

                So [0] is never to be used. I suspect @sierdzio was intending arguments()[1] maybe.

                However, while this may work for the OP's situation, other readers should be aware of the following. You can never be sure "which filename was clicked on in Explorer to launch application" in Windows (I suspect Linux desktops too).

                When you make a "File Association" under Windows, it creates an entry in the Registry. IIRC, this is/can be a "command-line", in which the file being double-clicked on is referenced by something like %1. Although the default may be that this is the first argument (arguments()[1]), plenty of programs have preceding command-line options in their command in the registry, like -a -b "%1" (or a /print %1 for printing, for example). In this case argument 1 would be -a, not what the OP wants.

                Only your program knows where it expects Explorer to pass the file path of the file clicked on the command-line, depending on option processing.

                If you want to guess, the last rather then the first argument is probably advisable, e.g. arguments()[arguments().length - 1]. But you can't be sure.

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #8

                @JNBarchan said in How to find the path of the file that launches the Qt application from explorer:

                So [0] is never to be used. I suspect @sierdzio was intending arguments()[1] maybe.

                Yes, my bad, sorry! I've corrected the post.

                (Z(:^

                Q 1 Reply Last reply
                3
                • sierdzioS sierdzio

                  @JNBarchan said in How to find the path of the file that launches the Qt application from explorer:

                  So [0] is never to be used. I suspect @sierdzio was intending arguments()[1] maybe.

                  Yes, my bad, sorry! I've corrected the post.

                  Q Offline
                  Q Offline
                  QtVik
                  wrote on last edited by
                  #9

                  @sierdzio @JNBarchan @mrjj :

                  Thanks for your inputs.
                  But this line is causing an assertion failure "index out of range" in qlist.h line#510
                  qDebug() << "File path:" << args.at(1);

                  Qt version used is 5.5.1

                  Thanks

                  sierdzioS 1 Reply Last reply
                  0
                  • Q QtVik

                    @sierdzio @JNBarchan @mrjj :

                    Thanks for your inputs.
                    But this line is causing an assertion failure "index out of range" in qlist.h line#510
                    qDebug() << "File path:" << args.at(1);

                    Qt version used is 5.5.1

                    Thanks

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by sierdzio
                    #10

                    @QtVik said in How to find the path of the file that launches the Qt application from explorer:

                    @sierdzio @JNBarchan @mrjj :
                    But this line is causing an assertion failure "index out of range" in qlist.h line#510
                    qDebug() << "File path:" << args.at(1);

                    Try printing all args:

                    qDebug() << "File path:" << args;
                    

                    This will show you all arguments (apart from ones consumed by Qt).

                    (Z(:^

                    Q 1 Reply Last reply
                    0
                    • sierdzioS sierdzio

                      @QtVik said in How to find the path of the file that launches the Qt application from explorer:

                      @sierdzio @JNBarchan @mrjj :
                      But this line is causing an assertion failure "index out of range" in qlist.h line#510
                      qDebug() << "File path:" << args.at(1);

                      Try printing all args:

                      qDebug() << "File path:" << args;
                      

                      This will show you all arguments (apart from ones consumed by Qt).

                      Q Offline
                      Q Offline
                      QtVik
                      wrote on last edited by
                      #11

                      @sierdzio
                      Actually I want path of the file which is invoking my application not the path of my application.exe.

                      Example:
                      I have a file named "sample.dat" in "C:\Meas folder" and Qt application in "C:\QApplication\Qapp.exe"
                      Now if I double click on the "sample.dat " I should be able to find this path (C:\Meas folder) through my Qt application ie. Qapp.exe

                      Thanks

                      JonBJ sierdzioS 2 Replies Last reply
                      0
                      • Q QtVik

                        @sierdzio
                        Actually I want path of the file which is invoking my application not the path of my application.exe.

                        Example:
                        I have a file named "sample.dat" in "C:\Meas folder" and Qt application in "C:\QApplication\Qapp.exe"
                        Now if I double click on the "sample.dat " I should be able to find this path (C:\Meas folder) through my Qt application ie. Qapp.exe

                        Thanks

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #12

                        @QtVik
                        @mrjj's post earlier shows you him doing this.

                        You should still show us qDebug() << "File path:" << args as requested by @QtVik above.

                        1 Reply Last reply
                        0
                        • Q QtVik

                          @sierdzio
                          Actually I want path of the file which is invoking my application not the path of my application.exe.

                          Example:
                          I have a file named "sample.dat" in "C:\Meas folder" and Qt application in "C:\QApplication\Qapp.exe"
                          Now if I double click on the "sample.dat " I should be able to find this path (C:\Meas folder) through my Qt application ie. Qapp.exe

                          Thanks

                          sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #13

                          @QtVik said in How to find the path of the file that launches the Qt application from explorer:

                          Actually I want path of the file which is invoking my application not the path of my application.exe.

                          I know, that's what we are trying to help you with.

                          Actually, apart from using QCoreApplication::arguments(), you may also consider using QDir::currentPath().

                          (Z(:^

                          JonBJ 1 Reply Last reply
                          1
                          • sierdzioS sierdzio

                            @QtVik said in How to find the path of the file that launches the Qt application from explorer:

                            Actually I want path of the file which is invoking my application not the path of my application.exe.

                            I know, that's what we are trying to help you with.

                            Actually, apart from using QCoreApplication::arguments(), you may also consider using QDir::currentPath().

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #14

                            @sierdzio said in How to find the path of the file that launches the Qt application from explorer:

                            you may also consider using QDir::currentPath().

                            Do you have any evidence/knowledge that launching by double-clicking from Windows Explorer sets the application's current directory to that of the file double-clicked on??

                            sierdzioS 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @sierdzio said in How to find the path of the file that launches the Qt application from explorer:

                              you may also consider using QDir::currentPath().

                              Do you have any evidence/knowledge that launching by double-clicking from Windows Explorer sets the application's current directory to that of the file double-clicked on??

                              sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #15

                              @JNBarchan said in How to find the path of the file that launches the Qt application from explorer:

                              @sierdzio said in How to find the path of the file that launches the Qt application from explorer:

                              you may also consider using QDir::currentPath().

                              Do you have any evidence/knowledge that launching by double-clicking from Windows Explorer sets the application's current directory to that of the file double-clicked on??

                              Nope, it's just a guess.

                              (Z(:^

                              1 Reply Last reply
                              0
                              • Q QtVik

                                Hello,

                                I have a *.dat file that can be opened in my qt application.
                                Now i want to check, from the explorer which file format / path of the file that user is trying to use to launch my application.

                                How to achieve that ?

                                Best regards

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #16

                                @QtVik
                                If you would just show us your output from GetCommandLine(), QCoreApplication::instance()->arguments() and/or your main()'s argv, argc we could tell you what to do....

                                Q 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @QtVik
                                  If you would just show us your output from GetCommandLine(), QCoreApplication::instance()->arguments() and/or your main()'s argv, argc we could tell you what to do....

                                  Q Offline
                                  Q Offline
                                  QtVik
                                  wrote on last edited by
                                  #17

                                  @JNBarchan
                                  Following are the values for const QStringList args(QApplication::instance()->arguments()), argc ,argv:
                                  1] argc value is 1
                                  2] argv value is the application path .
                                  3] args.at(0) value is also application path .
                                  4] args.at(1) -> crash the application .

                                  The above values are shown when I double click on the application directly.

                                  But this is not what i want !

                                  I want path of the file that is trying to invoke my application from explorer.
                                  Example: from the previous post it should be "C:\Meas_folder\sample.dat"

                                  Note: I have set my Qapp.exe as default application to open for sample.dat

                                  Thanks

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • Q QtVik

                                    @JNBarchan
                                    Following are the values for const QStringList args(QApplication::instance()->arguments()), argc ,argv:
                                    1] argc value is 1
                                    2] argv value is the application path .
                                    3] args.at(0) value is also application path .
                                    4] args.at(1) -> crash the application .

                                    The above values are shown when I double click on the application directly.

                                    But this is not what i want !

                                    I want path of the file that is trying to invoke my application from explorer.
                                    Example: from the previous post it should be "C:\Meas_folder\sample.dat"

                                    Note: I have set my Qapp.exe as default application to open for sample.dat

                                    Thanks

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by jsulm
                                    #18

                                    @QtVik Please provide same information for the case you are interested in! It doesn't help at all if you double click your app and then post the results here: this is not the use case you're interested in, right?
                                    So, open you app from explorer with a data file and post the content of QApplication::instance()->arguments() here...

                                    "Note: I have set my Qapp.exe as default application to open for sample.dat" - then it should be enough to double click on sample.dat and post the output of QApplication::instance() here.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    JonBJ 1 Reply Last reply
                                    3
                                    • jsulmJ jsulm

                                      @QtVik Please provide same information for the case you are interested in! It doesn't help at all if you double click your app and then post the results here: this is not the use case you're interested in, right?
                                      So, open you app from explorer with a data file and post the content of QApplication::instance()->arguments() here...

                                      "Note: I have set my Qapp.exe as default application to open for sample.dat" - then it should be enough to double click on sample.dat and post the output of QApplication::instance() here.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #19

                                      @jsulm
                                      Exactly... sigh.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved