前言
在 QT 中,使用 QSettings 时,默认是将字符串以 Unicode 格式存储,而不是以 UTF-8 编码直接写入配置文件。因为涉及到配置文件,有些时候,配置信息由界面端进行写操作,而后台服务进程进行读取并处理,碰巧便于维护的要求,某些配置项需要中文时,就需要设置为UTF-8格式存储。
功能讲解
QString设置UTF-8编码
#include <QSettings>
#include <QDebug>void MainWindow::saveconfmsg(){QSettings settings("config.ini", QSettings::IniFormat);//打开文件QString savestr="测试";// 保存中文字符settings.setValue("ChineseText", savestr);
}void MainWindow::getconfmsg(){QSettings settings("config.ini", QSettings::IniFormat);//打开文件// 读取中文字符QString confText = settings.value("ChineseText").toString();qDebug() << confText ;}
通常,如果配置文件的配置项只是QT界面端进行调用的话,以上的保存和读取函数已经够用了,但此时config.ini配置文件中保存的内容是Unicode编码,如下图所示:
而且在测试过程中还发现了会出现多加上了双引号的情况,此时只需要设置为UTF-8方式,即可解决此类问题,代码如下:
#include <QSettings>
#include <QDebug>void MainWindow::saveconfmsg(){QSettings settings("config.ini", QSettings::IniFormat);//打开文件settings.setIniCodec("UTF-8");//-------设置为UTF-8方式QString savestr="测试";// 保存中文字符settings.setValue("ChineseText", savestr);
}void MainWindow::getconfmsg(){QSettings settings("config.ini", QSettings::IniFormat);//打开文件settings.setIniCodec("UTF-8");//-------设置为UTF-8方式// 读取中文字符QString confText = settings.value("ChineseText").toString();qDebug() << confText ;}
另外,本人测试了QTextCodec的方式无效
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
Unicode转utf-8
如果界面端已经把中文以Unicode编码方式写入到配置文件,后台服务程序又是普通的C++程序,这时就需要进行Unicode转utf-8
#include <vector>
#include <iomanip>
#include <chrono>
#include <string>
#include <dlfcn.h>
#include <sstream>
#include <fstream>
// 将 Unicode 码点转换为 UTF-8 字符串
std::string unicodeToUtf8(unsigned int codepoint) {std::string utf8;if (codepoint <= 0x7F) {utf8.push_back(static_cast<char>(codepoint));} else if (codepoint <= 0x7FF) {utf8.push_back(static_cast<char>((codepoint >> 6) | 0xC0));utf8.push_back(static_cast<char>((codepoint & 0x3F) | 0x80));} else if (codepoint <= 0xFFFF) {utf8.push_back(static_cast<char>((codepoint >> 12) | 0xE0));utf8.push_back(static_cast<char>(((codepoint >> 6) & 0x3F) | 0x80));utf8.push_back(static_cast<char>((codepoint & 0x3F) | 0x80));} else {utf8.push_back(static_cast<char>((codepoint >> 18) | 0xF0));utf8.push_back(static_cast<char>(((codepoint >> 12) & 0x3F) | 0x80));utf8.push_back(static_cast<char>(((codepoint >> 6) & 0x3F) | 0x80));utf8.push_back(static_cast<char>((codepoint & 0x3F) | 0x80));}return utf8;
}// 解析 Unicode 字符串并转换为 UTF-8
std::string convertUnicodeStringToUtf8(const std::string& input) {std::string result;bool bflag=false;char flag1='\\';char flag2='x';size_t strlen=input.size();for(size_t i=0;i<strlen;i++){char ch=input[i];if(ch==flag1) {bflag=true;continue;//printf("i=%ld\n",i);}if(bflag==true && ch==flag2){if(((i+2)<strlen && input[i+3]==flag1)||(i+3)==strlen){//数字类型:\x31\x32\x33std::string hexStr = input.substr(i + 1, 2);unsigned int codepoint = std::stoul(hexStr, nullptr, 16); // 转换为整数std::cout << hexStr << "=>" << unicodeToUtf8(codepoint) << std::endl;i+=2;result+=unicodeToUtf8(codepoint);}else if((i+5)<=strlen){std::string hexStr = input.substr(i + 1, 4);unsigned int codepoint = std::stoul(hexStr, nullptr, 16); // 转换为整数//std::cout << hexStr << "=>" << unicodeToUtf8(codepoint) << std::endl;i+=4;result+=unicodeToUtf8(codepoint);}}else{bflag=false;//printf("i=%ld ch=%c\n",i,ch);result+=ch;}}//std::cout << "=>" << result << std::endl;return result;
}int main(){Getbaseinfo(CONFIGPATH);//需要自己实现std::string utf8_hanzistr = convertUnicodeStringToUtf8(conf_hanzistr);//====转码}
注:以上的convertUnicodeStringToUtf8()未去掉双引号。