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
Forum Updated to NodeBB v4.3 + New Features

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 16.1k Views 1 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on 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
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on 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

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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 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?

          JonBJ 1 Reply Last reply
          0
          • J Joachim W

            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?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            0
            • JonBJ JonB

              @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 last edited by
              #10

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

              JonBJ 1 Reply Last reply
              0
              • J Joachim W

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 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 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.

                    JonBJ 1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      Joachim W
                      wrote on 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 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.

                        VRoninV 1 Reply Last reply
                        0
                        • J Joachim W

                          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.

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on 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

                            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.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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

                            • Login

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