读取.ini文件乱码的解决办法

void CGetConfigInfo::GetValueByKey(QSettings *seting, const QString &key, QString &value)
{
    if(seting != nullptr)
    {
        value = seting->value(key).toString();
    }
}

void CGetConfigInfo::GetValueByKey(QString path, const QString &key, QString &value)
{
    QSettings *configIniRead = new QSettings(path, QSettings::IniFormat);
    //configIniRead->setIniCodec("UTF-8");  //转换编码,加了这句代码就不会乱码了,但是需要文件以utf8的格式保存
    configIniRead->setIniCodec(QTextCodec::codecForName("GB2312"));
    GetValueByKey(configIniRead, key, value);
    delete configIniRead;
}

void CGetConfigInfo::CGetConfigInfo::test()
{
    QString value;
    QString key = QString("/ChineseText/12");
    QString path = QString("Config/ErrorType.ini");
    GetValueByKey(path, key, value);
    qDebug() << QString("12对应的错误类型:") + value;
}

你可能感兴趣的:(QT)