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 set current working directory so that QFile gets relative paths right
QtWS25 Last Chance

how to set current working directory so that QFile gets relative paths right

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 15.9k 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.
  • J Offline
    J Offline
    Joachim W
    wrote on 13 Mar 2018, 12:14 last edited by
    #1

    How to set the current working directory (CWD) to where the application was started from, so that QFile finds paths relative to the CWD?

    Following https://forum.qt.io/topic/88626, I changed my code into

     QString name = argv[1];
     QDir::setCurrent(qApp->applicationDirPath());
     QFile file(name);
    

    Let my application have the absolute path /home/A/app.
    Let an input file have the absolute path /home/B/data.

    Now this works:

    $ cd /home/A
    $ app ../B/data
    

    But not this:

    $ cd /home
    $ A/app B/data
    

    It fails because the current path is set to where the application resides (/home/A), not to where it was started from (/home). This makes no sense, as it conflicts with Unix standards and habits. How to get it right?

    J 1 Reply Last reply 13 Mar 2018, 12:44
    0
    • M Offline
      M Offline
      mranger90
      wrote on 13 Mar 2018, 12:21 last edited by
      #2

      From the documentation for applicationDirPath()

      Returns the directory that contains the application executable.
      

      So it sounds to me like its doing exactly what it's supposed to be doing.
      I guess you still have to do: A/app ../B/data

      J 1 Reply Last reply 13 Mar 2018, 12:29
      2
      • M mranger90
        13 Mar 2018, 12:21

        From the documentation for applicationDirPath()

        Returns the directory that contains the application executable.
        

        So it sounds to me like its doing exactly what it's supposed to be doing.
        I guess you still have to do: A/app ../B/data

        J Offline
        J Offline
        Joachim W
        wrote on 13 Mar 2018, 12:29 last edited by
        #3

        @mranger90, you are certainly correct about qApp->applicationDirPath().

        I meant to ask what to do instead. I definitely need to get command line arguments interpreted in the standard way.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mranger90
          wrote on 13 Mar 2018, 12:37 last edited by
          #4

          If you need to know where you are calling the executable from then you might try:

          qDir::absolutePath()
          

          instead of

          QApplication::applicationDirPath()
          
          1 Reply Last reply
          2
          • V Offline
            V Offline
            VRonin
            wrote on 13 Mar 2018, 12:38 last edited by
            #5

            QDir::setCurrent(qApp->applicationDirPath()); requires you to call A/app ../B/data as the argument is a path relative to A/app

            if you want the path to be ralative to the current directory (A/app B/data) just don't change it and delete QDir::setCurrent(qApp->applicationDirPath());

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • P Offline
              P Offline
              Pablo J. Rogina
              wrote on 13 Mar 2018, 12:41 last edited by
              #6

              @Joachim-W I guess you don't need to use QDir:setCurrent at all, since per documentation:

              The current directory is the last directory set with QDir::setCurrent() or, if that was never called, the directory at which this application was started at by the parent process

              so in your example

               A/app B/data
              

              your current directory is just the directory at which this application was started by the parent process i.e. /home and using just QFile file(name) (without calling setCurrent() previously) should get the desired file

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • J Joachim W
                13 Mar 2018, 12:14

                How to set the current working directory (CWD) to where the application was started from, so that QFile finds paths relative to the CWD?

                Following https://forum.qt.io/topic/88626, I changed my code into

                 QString name = argv[1];
                 QDir::setCurrent(qApp->applicationDirPath());
                 QFile file(name);
                

                Let my application have the absolute path /home/A/app.
                Let an input file have the absolute path /home/B/data.

                Now this works:

                $ cd /home/A
                $ app ../B/data
                

                But not this:

                $ cd /home
                $ A/app B/data
                

                It fails because the current path is set to where the application resides (/home/A), not to where it was started from (/home). This makes no sense, as it conflicts with Unix standards and habits. How to get it right?

                J Offline
                J Offline
                JonB
                wrote on 13 Mar 2018, 12:44 last edited by
                #7

                @Joachim-W said in how to set current working directory so that QFile gets relative paths right:

                How to set the current working directory (CWD) to where the application was started from

                As others have said, the current working directory is where the application was started from!

                1 Reply Last reply
                1
                • J Offline
                  J Offline
                  Joachim W
                  wrote on 13 Mar 2018, 12:56 last edited by
                  #8

                  sorry, @VRonin, @Pablo-J-Rogina, @JonB: your advice to simply delete the instruction

                  QDir::setCurrent(qApp->applicationDirPath());
                  

                  may be consistent with docs, and it is certainly consistent with what I originally had expected from Qt ... but empirically it does not work. If I delete said instruction, then relative paths are interpreted relative to my home directory. Which may coincide with neither the directory where the app resides in, nor the directory where it was started from.

                  Last hope for a simple answer: maybe my app is misconfigured by some insane CMake instruction? Are there environment variables that would overwrite the default interpretation of relative paths?

                  J 1 Reply Last reply 13 Mar 2018, 12:59
                  0
                  • J Joachim W
                    13 Mar 2018, 12:56

                    sorry, @VRonin, @Pablo-J-Rogina, @JonB: your advice to simply delete the instruction

                    QDir::setCurrent(qApp->applicationDirPath());
                    

                    may be consistent with docs, and it is certainly consistent with what I originally had expected from Qt ... but empirically it does not work. If I delete said instruction, then relative paths are interpreted relative to my home directory. Which may coincide with neither the directory where the app resides in, nor the directory where it was started from.

                    Last hope for a simple answer: maybe my app is misconfigured by some insane CMake instruction? Are there environment variables that would overwrite the default interpretation of relative paths?

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Mar 2018, 12:59 last edited by JonB
                    #9

                    @Joachim-W
                    Well it doesn't for me....
                    How are you starting/running your application? For example, are you invoking it from a desktop icon?
                    Print out what the current directory is on application start-up?

                    J 1 Reply Last reply 13 Mar 2018, 13:01
                    0
                    • J JonB
                      13 Mar 2018, 12:59

                      @Joachim-W
                      Well it doesn't for me....
                      How are you starting/running your application? For example, are you invoking it from a desktop icon?
                      Print out what the current directory is on application start-up?

                      J Offline
                      J Offline
                      Joachim W
                      wrote on 13 Mar 2018, 13:01 last edited by
                      #10

                      @JonB: I'm starting the application from the Linux command line.

                      J 1 Reply Last reply 13 Mar 2018, 13:02
                      0
                      • J Joachim W
                        13 Mar 2018, 13:01

                        @JonB: I'm starting the application from the Linux command line.

                        J Offline
                        J Offline
                        JonB
                        wrote on 13 Mar 2018, 13:02 last edited by JonB
                        #11

                        @Joachim-W
                        And, presumably, you're saying that "command line" is in a terminal, with a non-home current directory?
                        What does QDir::current() or QDir::currentPath() return?

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Pablo J. Rogina
                          wrote on 13 Mar 2018, 13:15 last edited by Pablo J. Rogina
                          #12

                          @Joachim-W with due respect, this is working for me (consistently with documentation) (Lubuntu, Qt 5.9, console application):

                          #include <QCoreApplication>
                          #include <QDebug>
                          #include <QDir>
                          #include <QFile>
                          
                          int main(int argc, char *argv[])
                          {
                              QCoreApplication a(argc, argv);
                          
                              qDebug() << QDir::currentPath();
                              QString name = argv[1];
                              QFile file(name);
                              qDebug() << file.open(QIODevice::ReadOnly | QIODevice::Text);
                          
                              return a.exec();
                          }
                          

                          with the following output:

                          pablo@lubuntu64:/home$ A/app B/data.txt 
                          "/home"
                          true
                          

                          just in case, the contents of corresponding folders A and B

                          pablo@lubuntu64:/home$ ls A
                          app
                          pablo@lubuntu64:/home$ ls B
                          data.txt
                          

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          3
                          • J Offline
                            J Offline
                            Joachim W
                            wrote on 13 Mar 2018, 13:18 last edited by
                            #13

                            thanks to @JonB's questions, I located the problem: the current directory changes while my application is running, namely when the singleton instance of Mainwin is created.

                            J 1 Reply Last reply 13 Mar 2018, 14:54
                            1
                            • J Offline
                              J Offline
                              Joachim W
                              wrote on 13 Mar 2018, 14:14 last edited by
                              #14

                              deep in the code, in an inappropriate location and for unkown reasons, there was a statement

                              QDir::setCurrent(QDir::homePath());
                              

                              which obviously caused the described behavior, confused me for days, and made me unjustly suspect Qt of doing insane things.

                              Thanks to you all for helping me in interpreting the Qt docs and in locating the bogous instruction.

                              1 Reply Last reply
                              1
                              • J Offline
                                J Offline
                                Joachim W
                                wrote on 13 Mar 2018, 14:23 last edited by
                                #15

                                The ultimate cause of my problem is a conflict between command-line interface and GUI:

                                For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.

                                V 1 Reply Last reply 13 Mar 2018, 14:28
                                0
                                • J Joachim W
                                  13 Mar 2018, 14:23

                                  The ultimate cause of my problem is a conflict between command-line interface and GUI:

                                  For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.

                                  V Offline
                                  V Offline
                                  VRonin
                                  wrote on 13 Mar 2018, 14:28 last edited by
                                  #16

                                  @Joachim-W said in how to set current working directory so that QFile gets relative paths right:

                                  For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.

                                  QFileDialog has setDirectory there is no need to mess up the working directory

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  1 Reply Last reply
                                  4
                                  • J Joachim W
                                    13 Mar 2018, 13:18

                                    thanks to @JonB's questions, I located the problem: the current directory changes while my application is running, namely when the singleton instance of Mainwin is created.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 13 Mar 2018, 14:54 last edited by
                                    #17

                                    @Joachim-W said in how to set current working directory so that QFile gets relative paths right:

                                    thanks to @JonB's questions, I located the problem: the current directory changes while my application is running, namely when the singleton instance of Mainwin is created.

                                    Yep, that's the only thing which makes sense!

                                    I meant to ask what to do instead. I definitely need to get command line arguments interpreted in the standard way.

                                    Now, doubtless you already realise this, but if you are dealing with relative paths received from the command line, you'd better deal with them (even if it's only to make them absolute) before you hit that code which changes the working directory (unless they really intentionally relative to the home directory in your app for some reason).

                                    whereas for a command-line interface it is insane to overwrite the current working directory.

                                    I see no reason why a file dialog for read/write should not be relative to the current directory, rather than say the home directory, but of course depends on context.

                                    Finally, as @VRonin says, you can (and should) use QFileDialog::setDirectory() rather than actually changing directory if you only want that for the purpose of the file dialog.

                                    1 Reply Last reply
                                    3

                                    4/17

                                    13 Mar 2018, 12:37

                                    13 unread
                                    • Login

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