Results 1 to 18 of 18

Thread: QSettings problem on windows 7

  1. #1
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings problem on windows 7

    Hi, I have written a program that lets the users save their projects using QSettings (ini file). I use a file dialog so that the user can store it where ever they want. Everything works nice on windows xp, but on windows 7 all files ends up being stored in my programs folder in program files (x86). How can I avoid this? Do I have to include some sort of manifest and specify Administrator rights? If so, how do I do that? Should the manifest be compiled into the exe file or just be added as a seperate file? I compile using qmake and mingw, don`t want to use Visual studio
    -ivar

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSettings problem on windows 7

    Without you going into details on how you use QSettings, I will point you to the documentation:

    The paths for the .ini and .conf files can be changed using setPath(). On Unix and Mac OS X, the user can override them by by setting the XDG_CONFIG_HOME environment variable; see setPath() for details.

  3. #3
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    I construct it like this:
    settings = new QSettings( fileName, QSettings::IniFormat, this );
    Then set values and sync.
    fileName is the full path to the file. As mentioned it works fine on windows xp. It should not be nescessary to use setPath() when I specify the full path to the file

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSettings problem on windows 7

    QSetting is not a file; it is a persistent object. How it gets stored and retrieved is system dependent, and not all systems store it as a file by default. You can override the default behavior, as already noted, but if you're not willing to take that step, you get what the system gives you, and on many systems the path is either meaningless or ignored in favor of default locations.

    Nonetheless, you can override the default behavior if you choose to. But it requires a single additional statement on your part.

  5. #5
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    I know it is not a file, look at my previous post. As mentioned I construct the object by
    settings = new QSettings( fileName, QSettings::IniFormat, this );
    I thought this was enough to tell QSetting that I want a ini file? What single additional statement are you talking about?
    After setting values and calling settings->sync() the object is then supposed to save a ini file with the name specified in fileName (full path to file). This works perfectly in windows xp, the ini file is saved to where ever I want. But not in windows 7, where the ini file always ends up in the programs folder in program files (x86)

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    Windows 7 is far more restrictive than WinXP. You'll also notice that although the files look like they are in the Program Files folder, they are actually in a folder called "Virtual Store" thanks to the WinXP compatibility mode it turns on when it runs your program. Normally you can't write to the Program Files folder (and many other directories) under Win7.

    If you save the ini file in your documents folder, it should save successfully.

    However, note that ini files are obsolete and have been for many years. You should really allow QSettings to use the native format.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,534
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings problem on windows 7

    You will need you application to gain admin rights if you insist on writing the INI file to the program's install directory or any of the other protected locations on Windows 7. What actual file name value are you passing to QSettings?

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    Even if you give the program admin rights, it'll still show the "Do you want this program to makes changes to your computer?" prompt every time you run it. The easiest way is to just become Windows 7 compatibile rather than relying on compatibility modes that may not exist in Windows 8.

  9. #9
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    I would really like to be able to write the QSettings to a file. Even if ini files are old and obsolete, it works. I use it to save the users projects from my app. I want the users to be able to save their projects as files and then open them easily again later with a filedialog. I could write my own class to save/open my apps project files, but QSettings already existed and worked nice on xp. Squidge, what do you mean by "becoming windows 7 compatible"? What do I need to do to become windows 7 compatible? Qt should write a post about deploying qt apps on windows 7/vista. I have noted the virtual store stuff, quite annoying and unnecessary if you ask me.
    ChrisW67: I`m using a filedialog to let the user choose the filename. I use qDebug to see what file name is generated, and they look ok. As mentioned, it works on xp.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,534
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings problem on windows 7

    I have no problems with an INI file being written by Windows 7 to any allowable location (code below). The location you are reporting is not the default location for QSettings file store, so something about how you are setting the QSettings path is likely the problem. Attempting to write an INI file to the program's install directory screams two things to me;
    • the file name is not fully qualified, and
    • the program's install directory is the default working directory for application.


    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QString fileName =
    8. QFileDialog::getSaveFileName(0,
    9. "Save settings",
    10. QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation),
    11. "Settings files (*.ini)" );
    12. qDebug() << fileName; // Fully qualified path is output
    13.  
    14. QSettings s(fileName, QSettings::IniFormat);
    15. s.setValue("TestKey", "TestValue");
    16. s.sync();
    17.  
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    That is the way I do it also, as shown in your code. I should maybe have mentioned that the program I`m having trouble with on windows 7 is build on windows xp, could that maybe explain it...?

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,534
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings problem on windows 7

    Nope, that's how I did it too. Does the code I posted work on your machine?

  13. #13
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    Quote Originally Posted by ChrisW67 View Post
    Nope, that's how I did it too. Does the code I posted work on your machine?
    Damn... That worked, I could write an ini file to where ever I wanted from where ever I wanted on windows 7. Can`t see any significant differences between your code and mine. Will have to investigate this, I guess there is something with my code somewhere. Thanks for helping

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,534
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings problem on windows 7

    I'd be looking for exactly what value was being used in the QSettings constructor and whether you were using a full path all the time, i.e. not using a bare file name or relative path in some places.

  15. #15
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings problem on windows 7

    Found a bug... Everything is ok now. Thanks for helping

  16. #16
    Join Date
    Sep 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QSettings problem on windows 7

    so what was the bug?

  17. #17
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSettings problem on windows 7

    I had the same problem! After reading, searching ... i found the solution.
    After installing my app, i changed its shortcut on desktop to "run as administrator". And everything's fine.

    http://www.sevenforums.com/tutorials...nistrator.html

    PS: if i misunderstand the problem above, please let me know. Thanks.

    Regards.

  18. #18
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,534
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings problem on windows 7

    PS: if i misunderstand the problem above, please let me know. Thanks.
    The solution to the problem of writing a user settings file in a restricted system location is not to circumvent all security... it is to write the file in a permitted location. There are several of these, and they can be found using QDesktopServices::storageLocation() or by using Windows API calls directly.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. QSettings and Windows 7 64bit
    By JohnToddSr in forum Newbie
    Replies: 4
    Last Post: 12th May 2010, 02:01
  2. QSettings problem!
    By 0xl33t in forum Newbie
    Replies: 5
    Last Post: 28th July 2009, 08:15
  3. Problem with QSettings
    By arbi in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2008, 08:49
  4. Replies: 1
    Last Post: 19th September 2008, 15:43
  5. QSettings problem?
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2008, 21:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.