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"
Servers for Qt installer are currently down

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

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 7 Posters 11.2k 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.
  • A Offline
    A Offline
    ademmler
    wrote on 20 Aug 2020, 08:31 last edited by ademmler
    #1

    How do I use QDir to retrieve the "path of a subdirectory" with native Separators.
    Especially with correct quotation of spaces in the file path.

    For test you need to create a directory structure "~/mytestapp/test me/some.file".
    When I run the code I get his result.

    rocket:~ ademmler$ /Users/ademmler/mytestapp/qtpathcmd ; exit;
    Path:  "/Users/ademmler" 
    Content:  (".", "..", "main.cpp", "main.o", "Makefile", "my dir", "qtpathcmd", 
    "qtpathcmd.pro",  "qtpathcmd.pro.user") 
    
    Path to native spearators "/Users/ademmler"
    

    As you see i get only the path to the starting directory.
    What do I need to use to get this

    On Mac OS X:  "/Users/ademmler/test me/"
    On Windows:  "C:\Users\ademmler\test me\"
    

    thx in advance

    #include <QCoreApplication>
    #include <QDir>
    #include <QDebug>
    
    void exitApp(int r){
        QCoreApplication::exit(r);
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QDir mydir = QDir(QCoreApplication::applicationDirPath());
        mydir.cd("test me");
    
        qDebug() << "Path: " << mydir.currentPath() << Qt::endl;
        qDebug() << "Content: " << mydir.entryList() << Qt::endl;
        qDebug() << "Path to native spearators" << mydir.toNativeSeparators(mydir.currentPath()) << Qt::endl;
    
        return a.exec();
    }
    
    J 1 Reply Last reply 20 Aug 2020, 08:38
    0
    • A ademmler
      20 Aug 2020, 20:22

      @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

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Aug 2020, 00:06 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
      • A ademmler
        20 Aug 2020, 08:31

        How do I use QDir to retrieve the "path of a subdirectory" with native Separators.
        Especially with correct quotation of spaces in the file path.

        For test you need to create a directory structure "~/mytestapp/test me/some.file".
        When I run the code I get his result.

        rocket:~ ademmler$ /Users/ademmler/mytestapp/qtpathcmd ; exit;
        Path:  "/Users/ademmler" 
        Content:  (".", "..", "main.cpp", "main.o", "Makefile", "my dir", "qtpathcmd", 
        "qtpathcmd.pro",  "qtpathcmd.pro.user") 
        
        Path to native spearators "/Users/ademmler"
        

        As you see i get only the path to the starting directory.
        What do I need to use to get this

        On Mac OS X:  "/Users/ademmler/test me/"
        On Windows:  "C:\Users\ademmler\test me\"
        

        thx in advance

        #include <QCoreApplication>
        #include <QDir>
        #include <QDebug>
        
        void exitApp(int r){
            QCoreApplication::exit(r);
        }
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QDir mydir = QDir(QCoreApplication::applicationDirPath());
            mydir.cd("test me");
        
            qDebug() << "Path: " << mydir.currentPath() << Qt::endl;
            qDebug() << "Content: " << mydir.entryList() << Qt::endl;
            qDebug() << "Path to native spearators" << mydir.toNativeSeparators(mydir.currentPath()) << Qt::endl;
        
            return a.exec();
        }
        
        J Offline
        J Offline
        JonB
        wrote on 20 Aug 2020, 08:38 last edited by JonB
        #2

        @ademmler
        I have tried to explain to you before: Qt is not going to put in quotation marks, or backslashes, for you around paths with spaces. (It would do so if you passed the string to QProcess as an argument, but that's for that purpose only.)

        But I need this: "Path to native spearators:

        On Mac OS X: "/Users/ademmler/test\ me/some.file"

        On Windows: "/Users/ademmler/test me/some.file"

        Then you do not want native separators at all! Windows "/Users/ademmler/test me/some.file" is most certainly not using native separators!

        And you should not need /Users/ademmler/test\ me/some.file under Mac anyway, "/Users/ademmler/test me/some.file" should do just as well. Note that the second one is using " (double quote) marks around the path with a space in it, instead of putting in a \ (backslash). The two are equivalent under Linux, and so I presume for MacOS too. Double-quote marks work for Windows paths with spaces, but embedded backslash will not work, unlike Linux.

        Nothing in Qt will produce exactly what you write above for the two different operating systems.

        For that matter, I don't know why you want these two final paths.

        A 2 Replies Last reply 20 Aug 2020, 08:44
        1
        • J JonB
          20 Aug 2020, 08:38

          @ademmler
          I have tried to explain to you before: Qt is not going to put in quotation marks, or backslashes, for you around paths with spaces. (It would do so if you passed the string to QProcess as an argument, but that's for that purpose only.)

          But I need this: "Path to native spearators:

          On Mac OS X: "/Users/ademmler/test\ me/some.file"

          On Windows: "/Users/ademmler/test me/some.file"

          Then you do not want native separators at all! Windows "/Users/ademmler/test me/some.file" is most certainly not using native separators!

          And you should not need /Users/ademmler/test\ me/some.file under Mac anyway, "/Users/ademmler/test me/some.file" should do just as well. Note that the second one is using " (double quote) marks around the path with a space in it, instead of putting in a \ (backslash). The two are equivalent under Linux, and so I presume for MacOS too. Double-quote marks work for Windows paths with spaces, but embedded backslash will not work, unlike Linux.

          Nothing in Qt will produce exactly what you write above for the two different operating systems.

          For that matter, I don't know why you want these two final paths.

          A Offline
          A Offline
          ademmler
          wrote on 20 Aug 2020, 08:44 last edited by
          #3

          @JonB

          I corrected my typo about "" on windows ;-)

          1 Reply Last reply
          0
          • J JonB
            20 Aug 2020, 08:38

            @ademmler
            I have tried to explain to you before: Qt is not going to put in quotation marks, or backslashes, for you around paths with spaces. (It would do so if you passed the string to QProcess as an argument, but that's for that purpose only.)

            But I need this: "Path to native spearators:

            On Mac OS X: "/Users/ademmler/test\ me/some.file"

            On Windows: "/Users/ademmler/test me/some.file"

            Then you do not want native separators at all! Windows "/Users/ademmler/test me/some.file" is most certainly not using native separators!

            And you should not need /Users/ademmler/test\ me/some.file under Mac anyway, "/Users/ademmler/test me/some.file" should do just as well. Note that the second one is using " (double quote) marks around the path with a space in it, instead of putting in a \ (backslash). The two are equivalent under Linux, and so I presume for MacOS too. Double-quote marks work for Windows paths with spaces, but embedded backslash will not work, unlike Linux.

            Nothing in Qt will produce exactly what you write above for the two different operating systems.

            For that matter, I don't know why you want these two final paths.

            A Offline
            A Offline
            ademmler
            wrote on 20 Aug 2020, 08:45 last edited by
            #4

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

            For that matter, I don't know why you want these two final paths.

            Because launching an external command does not work as you described!

            J KroMignonK 2 Replies Last reply 20 Aug 2020, 08:53
            0
            • A ademmler
              20 Aug 2020, 08:45

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

              For that matter, I don't know why you want these two final paths.

              Because launching an external command does not work as you described!

              J Offline
              J Offline
              JonB
              wrote on 20 Aug 2020, 08:53 last edited by
              #5

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

              Because launching an external command does not work as you described!

              Yes it does. Maybe your code is incorrect?

              The fact remains that MacOs/LInux should be just as happy with passing "/Users/ademmler/test me/some.file" as with /Users/ademmler/test\ me/some.file.

              I and loads of people happily use toNativeSeparators() for paths with spaces to pass to QProcess across platforms.

              1 Reply Last reply
              1
              • A ademmler
                20 Aug 2020, 08:45

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

                For that matter, I don't know why you want these two final paths.

                Because launching an external command does not work as you described!

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on 20 Aug 2020, 13:35 last edited by
                #6

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

                Because launching an external command does not work as you described!

                I have used QProcess to launch external application on Linux, Windows and Android system and it always works.
                I never had issues with spaces or path separator.

                I think the issue is in the way to use QProcess.
                If you want help, you have to show the code which is not working, not basic usage of QDir.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • Cobra91151C Online
                  Cobra91151C Online
                  Cobra91151
                  wrote on 20 Aug 2020, 16:45 last edited by Cobra91151
                  #7

                  Hello!

                  You can use the QDir::separator() (https://doc.qt.io/qt-5/qdir.html#separator) or QDir::toNativeSeparators() (https://doc.qt.io/qt-5/qdir.html#toNativeSeparators) methods. It will return: "/" under Unix and "\" under Windows.

                  Also, you can try to replace "/" with "\" and it will work, for example: qDebug() << "Path: " << mydir.currentPath().replace("/", "\\") << Qt::endl;

                  Happy coding!

                  A 1 Reply Last reply 20 Aug 2020, 18:11
                  0
                  • Cobra91151C Cobra91151
                    20 Aug 2020, 16:45

                    Hello!

                    You can use the QDir::separator() (https://doc.qt.io/qt-5/qdir.html#separator) or QDir::toNativeSeparators() (https://doc.qt.io/qt-5/qdir.html#toNativeSeparators) methods. It will return: "/" under Unix and "\" under Windows.

                    Also, you can try to replace "/" with "\" and it will work, for example: qDebug() << "Path: " << mydir.currentPath().replace("/", "\\") << Qt::endl;

                    Happy coding!

                    A Offline
                    A Offline
                    ademmler
                    wrote on 20 Aug 2020, 18:11 last edited by
                    #8

                    @Cobra91151 In another post I have made before - it got told "never" to use QDir::Separator();

                    I wrote this minimal app to learn and understand how QDir really works.

                    @KroMignon I split my question into multiple posts, because they always belong to different problems and questions. Also I create in most cases a bare bone (minmal) app for testing.
                    I thought it is the recommended practice.

                    You find the issue about "launching a command" here:
                    https://forum.qt.io/topic/118236/qprocess-how-to-debug-the-full-command-send-used-to-start-the-process

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 20 Aug 2020, 18:42 last edited by
                      #9

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

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      A Cobra91151C 3 Replies Last reply 20 Aug 2020, 18:57
                      0
                      • Christian EhrlicherC Christian Ehrlicher
                        20 Aug 2020, 18:42

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

                        A Offline
                        A Offline
                        ademmler
                        wrote on 20 Aug 2020, 18:57 last edited by ademmler
                        #10

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

                        QDir::Separator();

                        ok - I understood - I will have a look at it agin.

                        A) Assume I have a folder in "/Users/ademmler/Desktop/myapplication/test me"

                        I start with: QDir mydir = QDir(QCoreApplication::applicationDirPath());
                        If I do mydir.cd("test me"); I would expect

                        mydir.toNativeSeparators(mydir.currentPath()) ? to give me
                        "/Users/ademmler/Desktop/myapplication/test me/"

                        Instead it shows "/Users/ademmler" only, which is the root where the application folder lays.

                        What I am doing wrong here?

                        And on top I was told that Process will deal with the correct path and separators automatically.
                        is this true or doe I need to prepare my arguments using "QDir::Separator(); ?

                        Christian EhrlicherC 1 Reply Last reply 20 Aug 2020, 19:30
                        0
                        • Christian EhrlicherC Christian Ehrlicher
                          20 Aug 2020, 18:42

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

                          A Offline
                          A Offline
                          ademmler
                          wrote on 20 Aug 2020, 18:58 last edited by
                          #11
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • A ademmler
                            20 Aug 2020, 18:57

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

                            QDir::Separator();

                            ok - I understood - I will have a look at it agin.

                            A) Assume I have a folder in "/Users/ademmler/Desktop/myapplication/test me"

                            I start with: QDir mydir = QDir(QCoreApplication::applicationDirPath());
                            If I do mydir.cd("test me"); I would expect

                            mydir.toNativeSeparators(mydir.currentPath()) ? to give me
                            "/Users/ademmler/Desktop/myapplication/test me/"

                            Instead it shows "/Users/ademmler" only, which is the root where the application folder lays.

                            What I am doing wrong here?

                            And on top I was told that Process will deal with the correct path and separators automatically.
                            is this true or doe I need to prepare my arguments using "QDir::Separator(); ?

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 20 Aug 2020, 19:30 last edited by Christian Ehrlicher
                            #12

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

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            A 2 Replies Last reply 20 Aug 2020, 19:42
                            0
                            • Christian EhrlicherC Christian Ehrlicher
                              20 Aug 2020, 19:30

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

                              A Offline
                              A Offline
                              ademmler
                              wrote on 20 Aug 2020, 19:42 last edited by
                              #13

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

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

                              Very good to know - thx.

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher
                                20 Aug 2020, 18:42

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

                                Cobra91151C Online
                                Cobra91151C Online
                                Cobra91151
                                wrote on 20 Aug 2020, 19:45 last edited by
                                #14

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

                                J A 2 Replies Last reply 20 Aug 2020, 19:54
                                0
                                • Cobra91151C Cobra91151
                                  20 Aug 2020, 19:45

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

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 20 Aug 2020, 19:54 last edited by JonB
                                  #15

                                  @Cobra91151
                                  It depends where/why you use it. I think the docs is pretty clear:

                                  Returns the native directory separator: "/" under Unix and "\" under Windows.

                                  You do not need to use this function to build file paths. If you always use "/", Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().

                                  While you manipulate paths within Qt you would be best using /, regardless of platform. So you don't need this. Only when you come to, say, passing a Qt-style path to an operating system command or displaying a message do you need to use QDir::separator() or toNativeSeparators().

                                  I happenstanced across https://agateau.com/2015/qdir-separator-considered-harmful/, that is short, clear (big writing, friendly font ;-) ) and illustrates the trap.

                                  1 Reply Last reply
                                  0
                                  • Cobra91151C Cobra91151
                                    20 Aug 2020, 19:45

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

                                    A Offline
                                    A Offline
                                    ademmler
                                    wrote on 20 Aug 2020, 20:01 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.

                                    J 1 Reply Last reply 20 Aug 2020, 20:18
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher
                                      20 Aug 2020, 19:30

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

                                      A Offline
                                      A Offline
                                      ademmler
                                      wrote on 20 Aug 2020, 20:06 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
                                      • A ademmler
                                        20 Aug 2020, 20:01

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

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 20 Aug 2020, 20:18 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().

                                        A 1 Reply Last reply 20 Aug 2020, 20:22
                                        0
                                        • J JonB
                                          20 Aug 2020, 20: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().

                                          A Offline
                                          A Offline
                                          ademmler
                                          wrote on 20 Aug 2020, 20:22 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 J 2 Replies Last reply 20 Aug 2020, 20:57
                                          0
                                          • A ademmler
                                            20 Aug 2020, 20:22

                                            @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 Online
                                            Cobra91151C Online
                                            Cobra91151
                                            wrote on 20 Aug 2020, 20:57 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().

                                            J 1 Reply Last reply 20 Aug 2020, 21:10
                                            0

                                            1/25

                                            20 Aug 2020, 08:31

                                            • Login

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