我在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输出
答案 0 :(得分:0)
看起来我只是在完全错误的轨道上.. INI文件转义工作正常。
\x....