Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Possible windows bug in "QStandardPaths"?



  • 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();   
    }
    


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



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



  • @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?



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



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



  • This post is deleted!


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



  • @KroMignon Thx for the perfect response I got it



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



  • @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?



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


  • @KroMignon thx again for this perfect answer


  • Moderators

    @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;
        }
    


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



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

Log in to reply