Search code examples
qtunicodeqsettings

QSettings & unicode


I have some buttons in a qml file and I want to make them editable from a settings file. Therefore I read my settings.ini with QSettings and pass the data to qml.

All works just fine until I try to read icons in unicode format. I found this question but the answer doesn't work for me.

The settings.ini has UTF-8 format and since "QString icon2 = QString::fromUtf8("\uf00c");" works, the problem has to be QSettings.

Any ideas?

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


[icons] icon1=\uF00C

Output:


icon1: "F00C" icon2: "\uF00C"

qml output


Solution

  • Looks like I was just on the totally wrong track.. INI file escaping works just fine.

    \x....