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
QtWS25 Last Chance

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.2k Views
  • 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.
  • S sierdzio
    28 Nov 2017, 13:04

    @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.

    J Offline
    J Offline
    JonB
    wrote on 28 Nov 2017, 13:28 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.

    M 1 Reply Last reply 28 Nov 2017, 15:34
    2
    • J JonB
      28 Nov 2017, 13:28

      @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.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Nov 2017, 15:34 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
      • J Offline
        J Offline
        JonB
        wrote on 28 Nov 2017, 17:25 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.

        S 1 Reply Last reply 28 Nov 2017, 18:56
        4
        • J JonB
          28 Nov 2017, 17:25

          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.

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 28 Nov 2017, 18:56 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 30 Nov 2017, 09:22
          3
          • S sierdzio
            28 Nov 2017, 18:56

            @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 30 Nov 2017, 09:22 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

            S 1 Reply Last reply 30 Nov 2017, 09:25
            0
            • Q QtVik
              30 Nov 2017, 09:22

              @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

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 30 Nov 2017, 09:25 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 30 Nov 2017, 10:52
              0
              • S sierdzio
                30 Nov 2017, 09:25

                @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 30 Nov 2017, 10:52 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

                J S 2 Replies Last reply 30 Nov 2017, 11:10
                0
                • Q QtVik
                  30 Nov 2017, 10:52

                  @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

                  J Offline
                  J Offline
                  JonB
                  wrote on 30 Nov 2017, 11:10 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
                    30 Nov 2017, 10:52

                    @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

                    S Offline
                    S Offline
                    sierdzio
                    Moderators
                    wrote on 30 Nov 2017, 11:17 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(:^

                    J 1 Reply Last reply 30 Nov 2017, 11:44
                    1
                    • S sierdzio
                      30 Nov 2017, 11:17

                      @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().

                      J Offline
                      J Offline
                      JonB
                      wrote on 30 Nov 2017, 11:44 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??

                      S 1 Reply Last reply 30 Nov 2017, 11:48
                      0
                      • J JonB
                        30 Nov 2017, 11:44

                        @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??

                        S Offline
                        S Offline
                        sierdzio
                        Moderators
                        wrote on 30 Nov 2017, 11:48 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
                          28 Nov 2017, 06:20

                          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

                          J Offline
                          J Offline
                          JonB
                          wrote on 30 Nov 2017, 11:51 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 1 Dec 2017, 04:18
                          0
                          • J JonB
                            30 Nov 2017, 11:51

                            @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 1 Dec 2017, 04:18 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

                            J 1 Reply Last reply 1 Dec 2017, 05:43
                            0
                            • Q QtVik
                              1 Dec 2017, 04:18

                              @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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 1 Dec 2017, 05:43 last edited by jsulm 12 Jan 2017, 05:44
                              #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

                              J 1 Reply Last reply 1 Dec 2017, 07:47
                              3
                              • J jsulm
                                1 Dec 2017, 05:43

                                @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.

                                J Offline
                                J Offline
                                JonB
                                wrote on 1 Dec 2017, 07:47 last edited by
                                #19

                                @jsulm
                                Exactly... sigh.

                                1 Reply Last reply
                                0

                                14/19

                                30 Nov 2017, 11:44

                                • Login

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