c++ - QSettings(Qt 5.4) : setValue doesn't work properly

标签 c++ qt qsettings

在我的 .cpp 中,我正在使用 QSettings。
这以前在 Qt 4.8 中有效:

#include <QSettings>


----------


QSettings settings;
settings.setValue("time_axis_direction", 1);
int test_var = settings.value("time_axis_direction").toInt();


----------

test_var程序返回0,请问是什么原因?
我将 Qt 与 VS 插件一起使用。

最佳答案

根据docs ,你必须设置组织名称和应用程序名称:

QCoreApplication::setOrganizationName("My Organization");
QCoreApplication::setApplicationName("My Application");
QSettings settings;

或者在构造函数中:

QSettings settings("My Organization", "My Application");

这将创建 HKCU\SOFTWARE\My Organization\My Application 注册表项来存储您的设置(在 Windows 上)。

If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.

关于c++ - QSettings(Qt 5.4) : setValue doesn't work properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822822/

相关文章:

python - 将Qcombobox的Qsettings写入文件而不关闭Widget

c++ - 为什么 C/C++ 代码可在不同的编译器上编译很重要?

c++ - 有没有办法解决供应商引入的#define?

c++ - 生成两个不同的随机数

c++ - Cython:将 C++ `std::complex<double>` 的 vector 转换为 C `double complex`

c++ - Qt/C++ 在 Mac OSX 10 上在哪里存储系统范围 QSettings?

qt - 使用 QSettings 保存 64 位整数

python - 连续 keydown 事件 Pyqt

c++ - Qt:主动聊天控件

c++ - 如何设计一个Qt前后端分离的程序?