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. QStandardPaths returning inconsistent directories (Windows)
Forum Update on Monday, May 27th 2025

QStandardPaths returning inconsistent directories (Windows)

Scheduled Pinned Locked Moved Solved General and Desktop
windowsqstandardpaths
10 Posts 3 Posters 2.5k 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.
  • SprezzaturaS Offline
    SprezzaturaS Offline
    Sprezzatura
    wrote on last edited by Sprezzatura
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • SprezzaturaS Offline
      SprezzaturaS Offline
      Sprezzatura
      wrote on last edited by
      #9

      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.

      1 Reply Last reply
      2
      • SprezzaturaS Sprezzatura

        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?

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

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

        1 Reply Last reply
        0
        • SprezzaturaS Offline
          SprezzaturaS Offline
          Sprezzatura
          wrote on last edited by Sprezzatura
          #3

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

          J.HilkJ JonBJ 2 Replies Last reply
          0
          • SprezzaturaS Sprezzatura

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

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #4

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


            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.

            1 Reply Last reply
            2
            • SprezzaturaS Sprezzatura

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

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

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

              1 Reply Last reply
              0
              • SprezzaturaS Offline
                SprezzaturaS Offline
                Sprezzatura
                wrote on last edited by Sprezzatura
                #6

                @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);
                
                J.HilkJ JonBJ 2 Replies Last reply
                0
                • SprezzaturaS Sprezzatura

                  @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);
                  
                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #7

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

                  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.

                  1 Reply Last reply
                  0
                  • SprezzaturaS Sprezzatura

                    @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);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

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

                    1 Reply Last reply
                    3
                    • SprezzaturaS Offline
                      SprezzaturaS Offline
                      Sprezzatura
                      wrote on last edited by
                      #9

                      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.

                      1 Reply Last reply
                      2
                      • SprezzaturaS Offline
                        SprezzaturaS Offline
                        Sprezzatura
                        wrote on last edited by Sprezzatura
                        #10

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

                        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