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

QStandardPaths returning inconsistent directories (Windows)



  • I am confused by the directory names returned by QStandardPaths::locate() in Windows. Several different types return the same directory. Some types return a different path at the beginning of program execution, than at the end, the latter being within the executable directory.

    Ex:


    [A] DataLocation (9), GenericDataLocation (11), ConfigLocation (13), GenericConfigLocation (16) and AppConfigLocation (18) all return the same C:/Users/username/AppData/Local/.


    [B] At the end of program execution, DataLocation (9), ConfigLocation (13), AppDataLocation (17) and AppConfigLocation (18) have been switched to \MyApp\Win32\Debug.


    I would expect consistent paths, I wouldn't expect to have to save the values at the beginning of a session.

    What am I missing?



  • OK, I think I've got it. This is what I called:

    QString qstr = QStandardPaths::locate(QStandardPaths::AppDataLocation, "", QStandardPaths::LocateDirectory);
    

    After scrutinizing the documentation, I tried:

    QStringList qstrList = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
    

    and qstrList[0] is consistently C:/Users/username/AppData/Roaming/

    Hurrah! Got what I needed. Thank you all for your contributions and inspiration. I continue to be delighted with Qt.



  • @Sprezzatura
    I don't know the answer, but just how early is "at the beginning of program execution"? How much Qt start-up has or has not happened by then? Something in your code happening "later" is presumably altering what they return.



  • Here's the main() code:

    int main(int argc, char *argv[])
    {
    	int ret;
    
    	loadAllOptions(rpd);	// first time we call QStandardPaths
    	
    	QApplication app(argc, argv);
    		
    	ChooseDialog *chooseDialog = new ChooseDialog(0);	// show a dialog
    	chooseDialog->show();
    	
    	ret = app.exec();
    	
    	saveAllOptions(rpd);	// second time we call QStandardPaths
    	return ret;
    }
    

    We don't change any of the QStandardPaths to the best of my knowledge (we're Qt newbies).


  • Moderators

    @Sprezzatura said in QStandardPaths returning inconsistent directories (Windows):

    QApplication

    The very first thing inside your main should be the creation of QApplication.

    I had something similar where I had QStandardPaths inside a static QString and that lead to all kinds of issue. I assume the creation of the static string was done before QApplication was finished creating.

    Because when I changed it, the problem went away.



  • @Sprezzatura
    I did not mean that your code changes paths, I meant (I am guessing that) they may get changed by whatever Qt does internally whenever.

    I think you should try printing them out immediately after your call to QApplication app(argc, argv);. Are they at that point as they were before that, or are they as they will be at the end of the app where you currently print them a second time?



  • @J-Hilk Good suggestion. Unfortunately, when I move the call to after:

    	QApplication app(argc, argv);
    	loadAllOptions(rpd);	// first time we call QStandardPaths
    
    

    AppDataLocation (9) returns \MyApp\Win32\Debug right off the bat. I want it to stay at C:/Users/username/AppData/Roaming/ like it was when I called it before

    QApplication app(argc, argv);
    

  • Moderators

    @Sprezzatura
    I can not confirm that.

    Calling

    qDebug() <<  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    

    returns the expected

    "C:/Users/<USER>/AppData/Roaming/<APPNAME>",

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    
        return a.exec();
    }
    


  • @Sprezzatura
    You should read through https://doc.qt.io/qt-5.9/qstandardpaths.html carefully, especially the example table of return results under Windows. E.g.

    AppDataLocation    "C:/Users/<USER>/AppData/Roaming/<APPNAME>", "C:/ProgramData/<APPNAME>", "<APPDIR>", "<APPDIR>/data"
    

    I presume your \MyApp\Win32\Debug matches "<APPDIR>". It will only get initialised to include this after QApplication() has been constructed. I assume you know these calls return a list of directories, and you are looking through each element.



  • OK, I think I've got it. This is what I called:

    QString qstr = QStandardPaths::locate(QStandardPaths::AppDataLocation, "", QStandardPaths::LocateDirectory);
    

    After scrutinizing the documentation, I tried:

    QStringList qstrList = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
    

    and qstrList[0] is consistently C:/Users/username/AppData/Roaming/

    Hurrah! Got what I needed. Thank you all for your contributions and inspiration. I continue to be delighted with Qt.



  • @JonB Noooo... I did not understand that the enumeration of multiple paths in the Windows example referred to a list. Now I know :o) thanks.


Log in to reply