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. Possible windows bug in "QStandardPaths"?
QtWS25 Last Chance

Possible windows bug in "QStandardPaths"?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.1k 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by ademmler
    #1

    Hi,

    I use QStandardPaths to retrieve specific folder locations on Mac/Win.
    For testing I wrote this final code below.

    The result on windows is this:

    QDir::separator =  "\\"
    QStandardPaths::DesktopLocation =  "C:/Users/ademmler/Desktop"
    

    But I would expect this:

    QStandardPaths::DesktopLocation =  "C:\\Users\\ademmler\\Desktop"
    

    The results on Mac/OS X are fine:

    QDir::separator =  "/" 
    QStandardPaths::DesktopLocation =  "/Users/ademmler/Desktop"
    

    The minimal code for testing:

    #include <QCoreApplication>
    #include <QStandardPaths>
    #include <QDir>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        qDebug() << "QDir::separator = " << QString(QDir::separator()) << Qt::endl;
        qDebug() << "QStandardPaths::DesktopLocation = " << QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) << Qt::endl;
    
        return a.exec();   
    }
    
    1 Reply Last reply
    0
    • ademmlerA ademmler

      @Bonnie thx for response.

      Does this mean I always add a "/" - on all platforms - to paths - if I have to?

      Dummy example:
      QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
      QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
      ....

      And how do I properly quote "Spaces" in paths than?

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

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

      Equally you do not need to "quote spaces" while internal. You would only need that if e.g. passing to something external --- and QProcess does that on argument lists for you, if that's what you're using.

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #2

        No, Qt use "/" as path separator for all OS.
        QDir::separator will only be used in QDir::toNativeSeparators.

        ademmlerA 1 Reply Last reply
        0
        • B Bonnie

          No, Qt use "/" as path separator for all OS.
          QDir::separator will only be used in QDir::toNativeSeparators.

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

          @Bonnie thx for response.

          Does this mean I always add a "/" - on all platforms - to paths - if I have to?

          Dummy example:
          QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
          QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
          ....

          And how do I properly quote "Spaces" in paths than?

          JonBJ KroMignonK 2 Replies Last reply
          0
          • ademmlerA ademmler

            @Bonnie thx for response.

            Does this mean I always add a "/" - on all platforms - to paths - if I have to?

            Dummy example:
            QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
            QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
            ....

            And how do I properly quote "Spaces" in paths than?

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

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

            Equally you do not need to "quote spaces" while internal. You would only need that if e.g. passing to something external --- and QProcess does that on argument lists for you, if that's what you're using.

            1 Reply Last reply
            2
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #5

              @ademmler said in Possible windows bug in "QStandardPaths"?:

              QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"

              That's right!

              And how do I properly quote "Spaces" in paths than?

              Well, in most situations you don't need to do that.

              ademmlerA 1 Reply Last reply
              0
              • B Bonnie

                @ademmler said in Possible windows bug in "QStandardPaths"?:

                QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"

                That's right!

                And how do I properly quote "Spaces" in paths than?

                Well, in most situations you don't need to do that.

                ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • ademmlerA ademmler

                  @Bonnie thx for response.

                  Does this mean I always add a "/" - on all platforms - to paths - if I have to?

                  Dummy example:
                  QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
                  QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
                  ....

                  And how do I properly quote "Spaces" in paths than?

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #7

                  @ademmler said in Possible windows bug in "QStandardPaths"?:

                  Does this mean I always add a "/" - on all platforms - to paths - if I have to?
                  Dummy example:
                  QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
                  QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
                  ....
                  And how do I properly quote "Spaces" in paths than?

                  As written in QDir documentation, Qt always use internally '/' as path separator.
                  There are many help functions to work with path.
                  Your dummy example:

                  QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
                  QString myWorkingDir = QDir(myDesktopPath).filePath("Jobsfolder");
                  

                  When you need to use the path for external "tools" (for example as parameter for a QProcess), you should use QDir:toNativeSeparators() so you don't have to carry about the path separator.

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

                  ademmlerA 1 Reply Last reply
                  1
                  • KroMignonK KroMignon

                    @ademmler said in Possible windows bug in "QStandardPaths"?:

                    Does this mean I always add a "/" - on all platforms - to paths - if I have to?
                    Dummy example:
                    QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
                    QString myWorkingDir = myDesktopPath + "/" + "Jobsfolder"
                    ....
                    And how do I properly quote "Spaces" in paths than?

                    As written in QDir documentation, Qt always use internally '/' as path separator.
                    There are many help functions to work with path.
                    Your dummy example:

                    QString myDesktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
                    QString myWorkingDir = QDir(myDesktopPath).filePath("Jobsfolder");
                    

                    When you need to use the path for external "tools" (for example as parameter for a QProcess), you should use QDir:toNativeSeparators() so you don't have to carry about the path separator.

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

                    @KroMignon Thx for the perfect response I got it

                    1 Reply Last reply
                    0
                    • ademmlerA Offline
                      ademmlerA Offline
                      ademmler
                      wrote on last edited by
                      #9

                      @KroMignon said in Possible windows bug in "QStandardPaths"?:

                      QDir(myDesktopPath).filePath("Jobsfolder");

                      Last question about all this:

                      On Mac OS X app resources are in the bundle package "Resources" folder.
                      I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than.

                      JonBJ KroMignonK J.HilkJ 3 Replies Last reply
                      0
                      • ademmlerA ademmler

                        @KroMignon said in Possible windows bug in "QStandardPaths"?:

                        QDir(myDesktopPath).filePath("Jobsfolder");

                        Last question about all this:

                        On Mac OS X app resources are in the bundle package "Resources" folder.
                        I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than.

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

                        @ademmler said in Possible windows bug in "QStandardPaths"?:

                        QDir(QApplication::applicationDirPath()).filePath("../Resources")

                        Be careful. I don't know anything about Mac or " in the bundle package "Resources" folder.". Is Resources a real, physical sub-directory located (one directory above?) your application directory, or is it purely an internal reference to the resources embedded in the executable?

                        ademmlerA 1 Reply Last reply
                        0
                        • ademmlerA ademmler

                          @KroMignon said in Possible windows bug in "QStandardPaths"?:

                          QDir(myDesktopPath).filePath("Jobsfolder");

                          Last question about all this:

                          On Mac OS X app resources are in the bundle package "Resources" folder.
                          I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by KroMignon
                          #11

                          @ademmler said in Possible windows bug in "QStandardPaths"?:

                          I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than

                          I would do it like this:

                          QDir resourcesPath(QApplication::applicationDirPath());
                          resourcesPath.cd("../Resources");
                          
                          // now you can use resourcesPath to get files from your resources directory.
                          

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

                          ademmlerA 1 Reply Last reply
                          1
                          • KroMignonK KroMignon

                            @ademmler said in Possible windows bug in "QStandardPaths"?:

                            I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than

                            I would do it like this:

                            QDir resourcesPath(QApplication::applicationDirPath());
                            resourcesPath.cd("../Resources");
                            
                            // now you can use resourcesPath to get files from your resources directory.
                            
                            ademmlerA Offline
                            ademmlerA Offline
                            ademmler
                            wrote on last edited by
                            #12

                            @KroMignon thx again for this perfect answer

                            1 Reply Last reply
                            0
                            • ademmlerA ademmler

                              @KroMignon said in Possible windows bug in "QStandardPaths"?:

                              QDir(myDesktopPath).filePath("Jobsfolder");

                              Last question about all this:

                              On Mac OS X app resources are in the bundle package "Resources" folder.
                              I would do QDir(QApplication::applicationDirPath()).filePath("../Resources"); than.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by J.Hilk
                              #13

                              @ademmler

                              Ihm using this to get the appbundle path on Mac OS and the executable path on the other platforms

                              static const QString &ApplicationFolder (){
                              #ifdef Q_OS_MACOS
                                      auto getStringPath = []()->QString{
                                              CFURLRef url =(CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
                                              QDir d(QUrl::fromCFURL(url).path());
                                              d.cdUp();
                                              return  d.absolutePath();
                                      };
                                      static const QString path = getStringPath();
                              #else
                                      static const QString path = QCoreApplication::applicationDirPath();
                              #endif
                                      return  path;
                                  }
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              ademmlerA 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @ademmler said in Possible windows bug in "QStandardPaths"?:

                                QDir(QApplication::applicationDirPath()).filePath("../Resources")

                                Be careful. I don't know anything about Mac or " in the bundle package "Resources" folder.". Is Resources a real, physical sub-directory located (one directory above?) your application directory, or is it purely an internal reference to the resources embedded in the executable?

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

                                @JonB

                                It is a real physical folder and may other things - defined by apple
                                Qt says: https://doc.qt.io/qt-5/macos-deployment.html

                                1 Reply Last reply
                                1
                                • J.HilkJ J.Hilk

                                  @ademmler

                                  Ihm using this to get the appbundle path on Mac OS and the executable path on the other platforms

                                  static const QString &ApplicationFolder (){
                                  #ifdef Q_OS_MACOS
                                          auto getStringPath = []()->QString{
                                                  CFURLRef url =(CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
                                                  QDir d(QUrl::fromCFURL(url).path());
                                                  d.cdUp();
                                                  return  d.absolutePath();
                                          };
                                          static const QString path = getStringPath();
                                  #else
                                          static const QString path = QCoreApplication::applicationDirPath();
                                  #endif
                                          return  path;
                                      }
                                  
                                  ademmlerA Offline
                                  ademmlerA Offline
                                  ademmler
                                  wrote on last edited by
                                  #15

                                  @J-Hilk thx for your suggestion.

                                  I tried this way - because it is platform independent. On top using a QDir objects is giving lots of other handy functionality like .exists(); .remove(); .removeRecursively() and others.

                                  QDir resourcesPath = QDir(QApplication::applicationDirPath());
                                  #if defined Q_OS_MACX    
                                      resourcesPath.cd("../Resources");
                                  #else
                                      resourcesPath.cd("resources");
                                  #endif
                                  
                                  1 Reply Last reply
                                  0

                                  • Login

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