QSettings & unicode
QSettings & unicode
我在 qml 文件中有一些按钮,我想让它们可以从设置文件中编辑。
因此,我使用 QSettings 读取 settings.ini 并将数据传递给 qml。
在我尝试读取 unicode 格式的图标之前,一切正常。
我找到了这个 question,但答案对我不起作用。
settings.ini 具有 UTF-8 格式,因为 "QString icon2 = QString::fromUtf8("\uf00c");"有效,问题必须是 QSettings。
有什么想法吗?
main.cpp
----------
int main(int argc, char *argv[])
{
// locale code doesnt help
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QGuiApplication app(argc, argv);
QList<QObject*> dataList;
QSettings *settings = new QSettings("settings.ini", QSettings::IniFormat);
// settings code doesnt change anything
settings->setIniCodec(QTextCodec::codecForName("UTF-8"));
settings->beginGroup("icons");
// read settings
QString icon1 = settings->value("icon1").toString();
// test
QString icon2 = QString::fromUtf8("\uf00c");
settings->endGroup();
qDebug() << "icon1:" << icon1;
qDebug() << "icon2:" << icon2;
// to qml
MyData *data1 = new MyData();
data1->setIcon(icon1);
MyData *data2 = new MyData();
data2->setIcon(icon2);
dataList.append(data1);
dataList.append(data2);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("mydata", QVariant::fromValue(dataList));
engine.load(QUrl("qrc:/main.qml"));
return app.exec();
}
qml
----------
FontLoader { id: fontAwesome; source: "qrc:/fontawesome-webfont.ttf" }
Repeater {
model: mydata
Grid {
rows: 1
spacing: 5
Text {
text: index
color: "black"
}
Text {
font: fontAwesome.name
text: model.icon
color: "black"
}
}
}
settings.ini
[图标]
icon1=\uF00C
输出:
icon1: "F00C"
icon2: "\uF00C"
qml 输出
看来我完全走错了路..
INI 文件转义工作得很好。
\x....
我在 qml 文件中有一些按钮,我想让它们可以从设置文件中编辑。 因此,我使用 QSettings 读取 settings.ini 并将数据传递给 qml。
在我尝试读取 unicode 格式的图标之前,一切正常。 我找到了这个 question,但答案对我不起作用。
settings.ini 具有 UTF-8 格式,因为 "QString icon2 = QString::fromUtf8("\uf00c");"有效,问题必须是 QSettings。
有什么想法吗?
main.cpp
----------
int main(int argc, char *argv[])
{
// locale code doesnt help
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QGuiApplication app(argc, argv);
QList<QObject*> dataList;
QSettings *settings = new QSettings("settings.ini", QSettings::IniFormat);
// settings code doesnt change anything
settings->setIniCodec(QTextCodec::codecForName("UTF-8"));
settings->beginGroup("icons");
// read settings
QString icon1 = settings->value("icon1").toString();
// test
QString icon2 = QString::fromUtf8("\uf00c");
settings->endGroup();
qDebug() << "icon1:" << icon1;
qDebug() << "icon2:" << icon2;
// to qml
MyData *data1 = new MyData();
data1->setIcon(icon1);
MyData *data2 = new MyData();
data2->setIcon(icon2);
dataList.append(data1);
dataList.append(data2);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("mydata", QVariant::fromValue(dataList));
engine.load(QUrl("qrc:/main.qml"));
return app.exec();
}
qml
----------
FontLoader { id: fontAwesome; source: "qrc:/fontawesome-webfont.ttf" }
Repeater {
model: mydata
Grid {
rows: 1
spacing: 5
Text {
text: index
color: "black"
}
Text {
font: fontAwesome.name
text: model.icon
color: "black"
}
}
}
settings.ini
[图标] icon1=\uF00C
输出:
icon1: "F00C" icon2: "\uF00C"
qml 输出
看来我完全走错了路.. INI 文件转义工作得很好。
\x....