Qt 中解决乱码的方法
来源:互联网 发布:poi 数据有效性 编辑:程序博客网 时间:2024/06/01 19:52
解决应用程序中中文设置
在程序main.cpp中加入以下代码
#include
int main(int argc, char **argv)
{
QTextCodec *codec = QTextCodec::codecForName("GB2312");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
}
这样在程序其他地方就可以使用中文了, tr(“中文”) 或者直接使用“中文了;
解决读取ini文件中中文乱码
QSettings settings("xxxx.ini",QSettings::IniFormat);
settings.setIniCodec(QTextCodec::codecForName("GB2312")); //在此添加设置,即可读写ini文件中的中文
settings.beginGroup("company");//...
settings.endGroup();
解决读取中文文件中文的乱码
QFile file("xxxx.txt");
QTextStream stream(file,QIODevice::ReadOnly);
stream.setCodeC( QTextCodec::codecForName("GB2312") );
stream.readAll();
注:这篇文章以上是从网上搜索而来,本是想在 *.ini 文件中解决读取中文信息,可是上诉提及的 void QSettings::setIniCodec ( QTextCodec * codec ) 函数是Qt4.5才涉及到的,一直使用Qt4.3.1开发.... ini文件还在乱码中!
在Qt程序中使用Windows API读取ini设置信息:
#include <Windows.h>
char cfg_IniName[] = "..\\bin\\config.ini";char buf[1024];
::GetPrivateProfileStringA("local", "name", "error", buf, 1024, cfg_IniName);
QString strPcName = QString::fromLocal8Bit(buf);::GetPrivateProfileInt(L"local", L"port", 0, L"..\\bin\\config.ini");
在Qt程序中使用Windows API写入ini设置信息:
#include <Windows.h>;
#include <AtlBase.h>
USES_CONVERSION;
char cfg_IniName[] = "..\\bin\\config.ini";
LPCTSTR lpLocal = A2W("local");
LPCTSTR lpName = A2W("name");
LPCTSTR lpPcName = A2W("vic.MINg");
LPCTSTR lpFile = A2W(cfg_IniName);
::WritePrivateProfileString ( lpLocal, lpName, lpPcName, lpFile);
哈哈,例子也没带中文,让大家木乃了~
- Qt中解决乱码的方法
- Qt中解决乱码的方法
- ZT:Qt中解决乱码的方法
- Qt中解决乱码的方法
- Qt中解决乱码的方法
- Qt中解决乱码的方法
- Qt 中解决乱码的方法
- Qt中解决乱码的方法
- Qt中解决中文乱码的方法 (有改动)
- Qt中解决中文乱码的方法----编码
- Qt中解决中文乱码的方法----编码
- Qt中解决中文乱码的方法----编码
- Qt 5 中解决中文乱码的方法
- qt中乱码的一种解决思路
- 【Qt】]两种解决Qt5显示中文乱码的方法
- 【Qt】]两种解决Qt5显示中文乱码的方法
- cedega4中解决中文乱码的方法
- 解决servlet中中文乱码的方法
- 采用软件nginx实现web服务器集群
- 【C++资料免豆下载】大量教程+工具+源码下载地址汇总
- Android源码50例
- uva Mathematics :: Basic Problems
- 关于python2中的unicode和str以及python3中的str和bytes
- Qt 中解决乱码的方法
- 小米不是富士康的稻草
- Pop3协议Protocol类分析
- Visual Studio 2010——C#的ContexMenuStrip和RichTextBox控件的使用
- 【JAVA资料免费下载】158个JAVA免豆精品资料汇总——下载目录(转载)
- ubuntu下jthread 和jrtplib的安装
- MyEclipse设置编码方式
- 使用js操作本地或远程的excel
- Yii framwork crud 笔记一