PDA

View Full Version : QSettings problem on windows 7



ivareske
19th September 2010, 14:53
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

tbscope
19th September 2010, 15:28
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.

ivareske
19th September 2010, 17:26
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

SixDegrees
19th September 2010, 19:55
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.

ivareske
19th September 2010, 22:31
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)

squidge
19th September 2010, 23:51
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.

ChrisW67
19th September 2010, 23:52
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?

squidge
19th September 2010, 23:55
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.

ivareske
20th September 2010, 00:44
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.

ChrisW67
21st September 2010, 00:47
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.




#include <QtGui>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QString fileName =
QFileDialog::getSaveFileName(0,
"Save settings",
QDesktopServices::storageLocation(QDesktopServices ::DocumentsLocation),
"Settings files (*.ini)" );
qDebug() << fileName; // Fully qualified path is output

QSettings s(fileName, QSettings::IniFormat);
s.setValue("TestKey", "TestValue");
s.sync();

return 0;
}

ivareske
21st September 2010, 15:45
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...?

ChrisW67
22nd September 2010, 03:55
Nope, that's how I did it too. Does the code I posted work on your machine?

ivareske
25th September 2010, 00:11
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

ChrisW67
25th September 2010, 06:06
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.

ivareske
28th September 2010, 20:48
Found a bug... Everything is ok now. Thanks for helping

sandu
29th September 2010, 16:04
so what was the bug?

huyhoangfool
13th July 2012, 06:19
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/11841-run-administrator.html

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

Regards.

ChrisW67
13th July 2012, 07:46
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.