加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 阅读应用程序设置的最佳实践

发布时间:2020-12-16 10:35:49 所属栏目:百科 来源:网络整理
导读:在我的桌面应用程序中,我使用QSettings来保存各种应用程序值.例如,主表单位置,最近文档,连接参数以及一些改变应用程序行为方式的内容,例如“在信息对话框中不再显示此消息”. 我通常的处理方法是在应用程序启动时将所有设置读入结构或对象,并在应用程序关闭
在我的桌面应用程序中,我使用QSettings来保存各种应用程序值.例如,主表单位置,最近文档,连接参数以及一些改变应用程序行为方式的内容,例如“在信息对话框中不再显示此消息”.

我通常的处理方法是在应用程序启动时将所有设置读入结构或对象,并在应用程序关闭时保存它们.

我还有一个对话框,可以让我编辑设置对象中的许多值,并在关闭对话框时保存它们.

许多窗口和非可视对象都需要设置对象中的值.

做这个的最好方式是什么?

我已经开始将设置对象作为主窗口的成员,但后来我遇到了其他窗口需要访问主窗口成员的问题.

我确实认为我可以在自己的cpp文件中创建设置对象,只需#include需要的地方.但我不确定这是否可能或它的语法.

这种情况的最佳做法是什么?

解决方法

我建议总是使用 QSettings,避免补充结构或类.您可以在主体中设置应用程序名称,组织名称和组织管理.

来自QtDocs:

If you use QSettings from many places in your application,you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(),and then use the default QSettings constructor:

QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");

然后使用默认构造函数,您需要在其中加入属性:

QSettings settings;

QSettings objects can be created either on the stack or on the heap (i.e. using new). Constructing and destroying a QSettings object is very fast.

您可以在程序中的任何位置设置设置:

If there already exists a setting with the same key,the existing value is overwritten by the new value. For efficiency,the changes may not be saved to permanent storage immediately. (You can always call sync() to commit your changes.)

您也可以在不同的线程中使用它而不会出现问题:

QSettings is reentrant. This means that you can use distinct QSettings object in different threads simultaneously. This guarantee stands even when the QSettings objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one QSettings object,the change will immediately be visible in any other QSettings objects that operate on the same location and that live in the same process.

QSettings can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations. It uses advisory file locking and a smart merging algorithm to ensure data integrity. Note that sync() imports changes made by other processes (in addition to writing the changes from this QSettings).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读