Unable to change path of QSettings

时间:2017-05-16 09:24:05

标签: c++ qt qsettings

I use the c:\windows\winf32.ini to store software parameters, I can read it by doing this:

QSettings settings("c:/windows/winf32.ini", QSettings::IniFormat, Q_NULLPTR);

I can read the file with the absolute path but when I use setValue() and sync(), the file is not updated.
However, with relative path "winf32.ini" he write correctly the change but it is not in the c:\windows\ path.
By default, with the QSettings::IniFormat, the folowing files are used:

  1. FOLDERID_RoamingAppData\MySoft\Star Runner.ini
  2. FOLDERID_RoamingAppData\MySoft.ini
  3. FOLDERID_ProgramData\MySoft\Star Runner.ini
  4. FOLDERID_ProgramData\MySoft.ini

So to change it, I have tried this:

QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, "c:/windows");
QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "c:/windows");
QSettings settings("winf32.ini", QSettings::IniFormat, Q_NULLPTR);  

But settings is empty. I use setPath before declare my settings variable because in the QSettings doc, it says:

Warning: This function doesn't affect existing QSettings objects.

I don't see what is the problem.

1 个答案:

答案 0 :(得分:1)

这绝对是一个权限问题。您的用户可能具有管理权限,但由于UAC (User Account Control),您必须将这些权限明确授予任何新进程,否则该进程将以较低的授权级别执行。例如,这就是为什么Windows每次都要求您以管理员身份运行某些软件(例如安装程序)的原因,即使您的用户 是管理员。

要检查它,只需打开文件资源管理器,转到编译可执行文件的目录,右键单击可执行文件,然后选择“以管理员身份运行”。 Windows(实际上是UAC)会要求您确认操作。之后,检查您的INI文件是否已被修改。

您可以通过设置链接器标志强制可执行文件在每次运行时请求管理权限:

  • Visual Studio:转到项目的属性,然后链接器> 清单文件> UAC执行级别并将其设置为requireAdministrator。 (不要忘记为项目的所有配置设置它。)

    enter image description here

  • Qt Creator(qmake):将QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"添加到.pro文件中。

为了完整答案,我的测试使用以下代码执行:

#include <QSettings>

int main(int argc, char* argv[])
{
  QSettings settings("c:/windows/winf32.ini", QSettings::IniFormat);

  settings.setValue("hello", "world");

  return 0;
}
相关问题