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 use QDir(); to get a path " with native separators"
Forum Updated to NodeBB v4.3 + New Features

How to use QDir(); to get a path " with native separators"

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 7 Posters 11.8k Views 2 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.
  • Cobra91151C Cobra91151

    @Christian-Ehrlicher said in How to use QDir(); to get a path " with native separators":

    @ademmler said in How to use QDir(); to get a path " with native separators":

    it got told "never" to use QDir::Separator();

    You should never use it inside Qt but since you're calling an external program which expects the correct separators you must use it.

    Hello! I am curious, why not to use QDir::separator() inside Qt? I am using this method quite a bit in my apps on Windows and it works well. Thanks.

    ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by ademmler
    #16

    @Cobra91151

    Because a day - here in the forum - i got this informations:
    "Qt use "/" as path separator for all OS.
    QDir::separator will only be used in QDir::toNativeSeparators."

    "While you are using Qt path functions you should always use /, and that is what they will return to you. If you want the native separators QDir has to/fromNativeSeparartors(), but these should only be used if passing to something external, like an OS command."

    Hence for me it meant - never use "QDir::separator" it is Qt internal only ....

    What I know get is better to use something like this example:

    QString mypath =  QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";
    

    Thank you very much for making it more clear.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @ademmler said in How to use QDir(); to get a path " with native separators":

      And on top I was told that Process will deal with the correct path and separators automatically.

      This is wrong for the arguments you pass to the process - Qt will not modify them in any way.

      QDir mydir = QDir(QCoreApplication::applicationDirPath());

      Please take a look what mydir contains after this call. I would guess it's not. Then QDir::chdir() returns a bool which you should also check and at least QDir::toNativeSeparators() is a static function and needs no object. And it simply does a QString::replace('/', "\") on windows and QString::replace("\", '/') on linux so it can't be the reason for the wrong directory at all.

      ademmlerA Offline
      ademmlerA Offline
      ademmler
      wrote on last edited by
      #17

      @Christian-Ehrlicher said

      This is wrong for the arguments you pass to the process - Qt will not modify them in any way.

      But it will add "spaces" between all argument "peaces".

      Wich leads into this to be correct:
      arguments << "-opiton1" << "-ringthebell" << "yes" ... ;

      1 Reply Last reply
      0
      • ademmlerA ademmler

        @Cobra91151

        Because a day - here in the forum - i got this informations:
        "Qt use "/" as path separator for all OS.
        QDir::separator will only be used in QDir::toNativeSeparators."

        "While you are using Qt path functions you should always use /, and that is what they will return to you. If you want the native separators QDir has to/fromNativeSeparartors(), but these should only be used if passing to something external, like an OS command."

        Hence for me it meant - never use "QDir::separator" it is Qt internal only ....

        What I know get is better to use something like this example:

        QString mypath =  QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";
        

        Thank you very much for making it more clear.

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

        @ademmler said in How to use QDir(); to get a path " with native separators":

        QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";

        For anyone coming across this: As per the article referenced earlier, this is precisely the wrong place to use QDir::separator().

        ademmlerA 1 Reply Last reply
        0
        • JonBJ JonB

          @ademmler said in How to use QDir(); to get a path " with native separators":

          QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder";

          For anyone coming across this: As per the article referenced earlier, this is precisely the wrong place to use QDir::separator().

          ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by ademmler
          #19

          @JonB thx for responding.
          Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.

          Pls see above first comment from @Christian-Ehrlicher

          Cobra91151C JKSHJ 2 Replies Last reply
          0
          • ademmlerA ademmler

            @JonB thx for responding.
            Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.

            Pls see above first comment from @Christian-Ehrlicher

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #20

            @JonB @ademmler

            I have checked it on Windows, this path will work C:/test/path\test.exe even thought it is wrong. So, this code QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder"; in your case:

            QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) will have "/" and
            QDir::separator() + "my folder" will have "\my folder".

            It will work, but the path is not correct. Instead, there are a lot of options we can do here.
            QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\" + "my folder";
            QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\my folder";

            Or

            QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("/") + "my folder");

            QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("\\") + "my folder");

            QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/" + "my folder");

            QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "\\" + "my folder");

            QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder");

            The path should contain all "/" or "\" to be correct.
            All, these paths will return: "C:\\Users\\username\\Desktop\\my folder" and it is valid and correct for Windows, so you can use it to build paths and display to the user. The QDir::toNativeSeparators() method will display the correct "/" or "\" depending on the current OS, and will fix the path issue with QDir::separator().

            JonBJ 1 Reply Last reply
            0
            • Cobra91151C Cobra91151

              @JonB @ademmler

              I have checked it on Windows, this path will work C:/test/path\test.exe even thought it is wrong. So, this code QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder"; in your case:

              QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) will have "/" and
              QDir::separator() + "my folder" will have "\my folder".

              It will work, but the path is not correct. Instead, there are a lot of options we can do here.
              QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\" + "my folder";
              QString myPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\") + "\\my folder";

              Or

              QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("/") + "my folder");

              QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("\\") + "my folder");

              QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/" + "my folder");

              QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "\\" + "my folder");

              QString myPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator() + "my folder");

              The path should contain all "/" or "\" to be correct.
              All, these paths will return: "C:\\Users\\username\\Desktop\\my folder" and it is valid and correct for Windows, so you can use it to build paths and display to the user. The QDir::toNativeSeparators() method will display the correct "/" or "\" depending on the current OS, and will fix the path issue with QDir::separator().

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

              @Cobra91151
              That's a long answer! :)

              Yes, the issue will show up under Windows only. C:/test/path\test.exe is the kind of thing that will result from using QDir::separator(). Which looks wrong, and may (or may not) cause problems.

              The point is to just use / while passing a path around in Qt code. So

              QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/my folder";
              

              would be best.

              You only need QDir::separator() or toNativeSeparators() when you're e.g. passing to OS or printing.

              1 Reply Last reply
              3
              • ademmlerA ademmler

                @JonB thx for responding.
                Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.

                Pls see above first comment from @Christian-Ehrlicher

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #22

                @ademmler said in How to use QDir(); to get a path " with native separators":

                Maybe I am mistaken - but from the discussion above I got the recommendations to use QDir::separator(). Now I am not sure which answer is the correct one.

                Pls see above first comment from @Christian-Ehrlicher

                @Christian-Ehrlicher only said that you should use native separators when writing the path to an external non-Qt process.

                In your example, you are not writing the path to an external process. You are building up a path inside Qt. So, QDir::separator() is not appropriate here.

                The correct way to build your path in a cross-platform way is QString mypath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/my folder"; like @JonB said.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                3
                • ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by ademmler
                  #23

                  @Christian-Ehrlicher @Cobra91151 @JonB @KroMignon and all the others:

                  This had been a very longish task - but I am glad about it - because it demystified some obstacles for me.
                  Somehow QProcess(); and QStandardPaths(); got mixed up - but that was very good for some reason.

                  Thx again - which answer shall I mark as the solution now? You ALL have been very helpful.

                  Another question came up into my mind. Whats about using QFile::copy(); function.
                  Does this take care of platform depended details (in path names) automatically ?

                  QString targetFile(myFolder.absolutePath() + "/" + base + "." + ext);
                  QFile sourceFile(inputFile);
                  bool    ok = sourceFile.copy(targetFile);
                  
                  aha_1980A 1 Reply Last reply
                  0
                  • ademmlerA ademmler

                    @Christian-Ehrlicher @Cobra91151 @JonB @KroMignon and all the others:

                    This had been a very longish task - but I am glad about it - because it demystified some obstacles for me.
                    Somehow QProcess(); and QStandardPaths(); got mixed up - but that was very good for some reason.

                    Thx again - which answer shall I mark as the solution now? You ALL have been very helpful.

                    Another question came up into my mind. Whats about using QFile::copy(); function.
                    Does this take care of platform depended details (in path names) automatically ?

                    QString targetFile(myFolder.absolutePath() + "/" + base + "." + ext);
                    QFile sourceFile(inputFile);
                    bool    ok = sourceFile.copy(targetFile);
                    
                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    Hi @ademmler,

                    Another question came up into my mind. Whats about using QFile::copy(); function.
                    Does this take care of platform depended details (in path names) automatically ?

                    Every Qt API takes forward slashes as separator. You only need native separators when you display a path to the user or when you pass it to some thirdparty API. (Side node: even the Windows API allows forward slashes in many cases).

                    So yes, QFile::copy takes care for the platform details itself.

                    Regards

                    Qt has to stay free or it will die.

                    ademmlerA 1 Reply Last reply
                    4
                    • aha_1980A aha_1980

                      Hi @ademmler,

                      Another question came up into my mind. Whats about using QFile::copy(); function.
                      Does this take care of platform depended details (in path names) automatically ?

                      Every Qt API takes forward slashes as separator. You only need native separators when you display a path to the user or when you pass it to some thirdparty API. (Side node: even the Windows API allows forward slashes in many cases).

                      So yes, QFile::copy takes care for the platform details itself.

                      Regards

                      ademmlerA Offline
                      ademmlerA Offline
                      ademmler
                      wrote on last edited by
                      #25

                      @aha_1980 thank you for proving this

                      1 Reply Last reply
                      1

                      • Login

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