我在Settings.bundle中有以下Root.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>TAP Engine Hostname: </string>
<key>DefaultValue</key>
<string>localhost</string>
<key>Key</key>
<string>engineHostname</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
问题是,当我查询engineHostname值时,它是一个空字符串。根据我在Apple开发者页面上的阅读,需要在用户首选项数据库中注册默认设置。任何人都可以建议如何使用Qt完成这项工作?
答案 0 :(得分:1)
我已经解决了这个问题。我正在寻找一种“Qt方式”来解决我的问题,但Qt确实提供了一种机制来做我想要的。 Qt允许项目文件包含Objective C文件,而Clang编译器允许混合使用C ++和Objective C.技巧是确保头文件具有C ++接口。我最终得到了以下内容:
//
// UserPreferenceUtility.h
// tap_new_display_app
//
// Created by David Roscoe on 5/21/15.
//
//
#ifndef tap_new_display_app_UserPreferenceUtility_h
#define tap_new_display_app_UserPreferenceUtility_h
class QString;
class UserPreferenceUtility
{
public:
/// register default preferences from plist file
void registerDefaultPrefs(void);
/// get currently stored engine hostname
QString getEngineHostname(void);
private: // methods
};
#endif
这提供了C ++接口。混合模式实现在这里:
//
// UserPreferenceUtility.mm
// tap_new_display_app
//
// Created by David Roscoe on 5/19/15.
//
//
#include "UserPreferenceUtility.h"
#import <Foundation/Foundation.h>
#include <QtCore>
// ----------------------------------------------------------------------------
void UserPreferenceUtility::registerDefaultPrefs()
{
NSDictionary* appDefaults = [NSDictionary
dictionaryWithObject:@"localhost" forKey:@"engineHostname"];
if (appDefaults)
{
// the default value was found in the dictionary. Register it.
// wrapping this in an if.. clause prevents overwriting a user entered
// value with the default value
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
}
}
// ----------------------------------------------------------------------------
QString UserPreferenceUtility::getEngineHostname()
{
NSString* engine_hostname_pref = [[NSUserDefaults standardUserDefaults] stringForKey:@"engineHostname"];
return QString::fromNSString(engine_hostname_pref);
}
C ++函数执行本机Objective C调用以提取存储在用户首选项数据库中的首选项。