gpt4 book ai didi

c++ - MacOS 上的 QSettings 奇怪行为

转载 作者:行者123 更新时间:2023-11-30 03:21:08 25 4
gpt4 key购买 nike

在 MacOS 10.13.6 上通过自制软件使用 Qt 5.11.1,我在我的应用程序中使用 QSettings。这是我正在从 Linux 和 Windows 移植到 MacOS 的工作程序。

我在 MacOS 上遇到的问题是它将文件保存到 /Users/michaelleonetti/Library/Preferences/com.sonarcloud.Sonarcloud Service.plist,这是应该的,在第一次执行程序时。

如果我删除文件有时它又回来了。 有时它不会。如果我用 Xcode 编辑 plist 文件并保存它,我在“have”中写入的值将不会存在。

我无法确定韵律或让它发挥作用的原因。

我创建了一个最小示例:

#include <QSettings>

#include <iostream>

int main( int argc, char *argv[] )
{

{ // Scope
QSettings settings( QSettings::NativeFormat, QSettings::UserScope, "My Application Company", "My Application" );
if( settings.contains( "have" ) )
std::cout << "We have: " << settings.value( "have" ).toString().toStdString() << std::endl;
else
std::cout << "You do not have" << std::endl;

settings.setValue( "version", 10 );
std::cout << "Path is: " << settings.fileName().toStdString() << std::endl;
}

return( EXIT_SUCCESS );
}

输出:

$ ./src/reproduce
You do not have
Path is: /Users/michaelleonetti/Library/Preferences/com.my-application-company.My Application.plist

文件检查

$ cat "/Users/michaelleonetti/Library/Preferences/com.my-application-company.My Application.plist"
cat: /Users/michaelleonetti/Library/Preferences/com.my-application-company.My Application.plist: No such file or directory

我怎样才能解决这个问题?

最佳答案

认为这是由于 QSettings 是在 Apple 的 CFPreferences 数据库之上实现的。这是预期的行为,“修复”是调用 CFPreferencesSynchronize()

文档:

writes all pending changes to preference data to permanent storage, and reads latest preference data from permanent storage

幸运的是,QT 似乎将其包装在其 Mac implementation 中对于 QSettings,您应该能够调用 QSettings::sync() 以与平台无关的方式执行此操作。

基本上,您需要先同步您的设置,然后才能看到来自外部写入的任何更改**。这也是为什么文件似乎并不总是被重写的原因(它应该在应用程序退出时自动刷新)。

请注意,您还需要通过在该应用程序中调用 CFPreferencesSynchronize() 从外部应用程序同步您的设置,以便查看您的 QT 应用程序所做的更改。

知道有一个名为 defaults 的命令行实用程序可能会很有趣,您可以使用它从 shell 脚本或终端执行同步写入。

** 这会在某些点自动发生。

关于c++ - MacOS 上的 QSettings 奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52299114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com