QSettings IniFormat文件重新启动后为空 [英] QSettings IniFormat File gets empty after restart

查看:153
本文介绍了QSettings IniFormat文件重新启动后为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QSettings在Linux上的ini文件中保存和加载参数:

I use QSettings to save and load parameters to/from an ini file using Linux:

写:

QSettings settings("setup.ini", QSettings::IniFormat);
settings.beginGroup("Setup_Parameter");
settings.setValue("Parameter1",parameter1_value);
settings.sync();
settings.endGroup();

阅读:

QSettings settings("setup.ini", QSettings::IniFormat);
settings.beginGroup("Setup_Parameter");
parameter1_value = settings.value("Parameter1","0").toInt();
settings.endGroup();

在系统启动时,setup.ini可以正常工作. 如果我通过关闭电源然后再打开来重新启动系统,则setup.ini文件有时会完全变空.我会说5次尝试中的3次.

The setup.ini works fine, while the system is on. If i reboot my system by switching power off and on again, the setup.ini file gets completely empty sometimes. I would say in 3 out of 5 trys.

我已经尝试将文件保存在应用程序和root/Settings路径中. 除了在写入文件后复制文件外,在关闭电源后再打开电源,副本也为空.

I already tryed saving the file in application and root/Settings path. As well as copying the file after writing it, but then also the copy is empty after power off and on.

为什么setup.ini文件会丢失其内容?重新启动时需要保留参数.

Why does the setup.ini File looses its content? It needs to keep the parameters while restart.

推荐答案

写入函数后需要sync():

A sync() was required after the write function:

QSettings settings("setup.ini", QSettings::IniFormat);
settings.beginGroup("Setup_Parameter");
settings.setValue("Parameter1",parameter1_value);
settings.endGroup();
settings.sync();
sync();

这篇关于QSettings IniFormat文件重新启动后为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆