QString 和 TCHAR 的相互转换
参考资料:
http://www.cnblogs.com/fuyanwen/p/3200536.html
http://www.cnblogs.com/wendao/archive/2012/07/27/2612597.html
不能直接用:
QString szqFileName = QString::fromLocal8Bit("data");
const char* szName = szqFileName.toLocal8Bit().constData(); // 返回的 QByteArray 被析构掉了, szName 会指向无效的内存区
正确的做法:
QString szqFileName = QString::fromLocal8Bit("data");
QByteArray ba = szqFileName.toLocal8Bit();
const char* szName = ba.constData();
代码(在我的系统上, Win7 x64. CHS. 第21, 40行如果我用utf8的话就会乱码), 下载DEMO:
- #include <iostream>
- #include <Windows.h>
- #include <QtCore/QtCore>
- #ifdef UNICODE
- #define FILE_NAME "数据.txt"
- #else
- #define FILE_NAME "data.txt"
- #endif
- int main()
- {
- QString szqFileName = QString::fromLocal8Bit(FILE_NAME);
- qint64 nLen = szqFileName.length();
- //
- // QString to TCHAR*
- //
- LPCTSTR szFileName = NULL;
- #ifdef UNICODE
- szFileName = (wchar_t*)szqFileName.utf16();
- #else
- QByteArray byteArray = szqFileName.toLocal8Bit();
- szFileName = byteArray.constData();
- #endif // UNICODE
- //
- // Create file
- //
- HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, , NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- std::cout << "Cannot create file." << std::endl;
- else
- std::cout << "File created." << std::endl;
- ::CloseHandle(hFile);
- //
- // TCHAR* to QString
- //
- szFileName = TEXT(FILE_NAME);
- #ifdef UNICODE
- szqFileName = QString::fromUtf16((ushort*)szFileName);
- #else
- szqFileName = QString::fromLocal8Bit(szFileName);
- #endif
- QFile file(szqFileName);
- if (!file.remove())
- std::cout << "Cannot remove file." << std::endl;
- else
- std::cout << "File removed." << std::endl;
- return ;
- }
QString 和 TCHAR 的相互转换的更多相关文章
- 【转】QT QString, wchar_t *, TCHAR, CString和其他字符或字符串类型的转化
//QString to wchar_t *: const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileNa ...
- QT5中QString与char *的相互转换
以例子说明: #include <QApplication> #include <QDebug> #include <QString> #include <Q ...
- QString与char*的相互转换
原地址:http://blog.sina.com.cn/s/blog_5c70dfc80100r0nh.html 一.QString转char* QString str; int num=0; s ...
- QString与string的相互转换
1.QString转换String string s = qstr.toStdString(); 2.String转换QString QString qstr2 = QString::fromStdS ...
- QString与string的相互转换【转载】
文章转载自https://blog.csdn.net/qq_33485434/article/details/80680506 1.QString转换String string s = qstr.to ...
- Qt QString 和 LPCWSTR 的相互转换
在windosw 编程中,常用到LPCWSTR 变量,QT中最常用到QString,下面提供QString和LPCWSTR 相互转换的方法 LPWSTR 转换成QString LPCWSTR str; ...
- QString, string, int, char* 之间相互转换
这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...
- 【原创】<Debug> QString
[问题1] 'class QString' has no member named 'toAscii' [解答] 把toAscii().data()改成toLatin1().data() 如果QStr ...
- QString转换为LPTSTR(使用了reinterpret_cast,真是叹为观止,但是也开阔了思路),三篇文章合起来的各种转换方法
醉了,windows下宏定义了很多char类型 LPTSTR .今天,直接使用,qt报错,真TM费事. 将“CPU”转化为wcha_t * QString str = "CPU"; ...
随机推荐
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- BOM表生成
- 1st-code-review summary
每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...
- Cacti安装详细步骤
原文链接: https://www.centos.bz/2012/01/cacti-install-tutorials/ Cacti-监控MySQL: http://www.cszhi.com/201 ...
- regardless of how many processors are devoted to a parallelized execution of this program
https://en.wikipedia.org/wiki/Amdah's_law Amdahl's law is often used in parallel computing to predic ...
- Nginx return 关键字配置小技巧
Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...
- ADS报错 Warning : L6301W:Could not find file C:\Program Files . Error : L6218 : Undefined symbol ......
ADS1.2编译时,出现找不到一个不存在目录下的目标文件(*.o) 编译一个COPY到硬盘上的一个工程,出现以下的fatal error message: Error: (Fatal)L6002: C ...
- C# .Net实现URL绝对路径和相对路径之间互相转换
网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...
- BlueDroid代码分析之GKI
目录 1. 概述 2. 线程 2.1 主要函数 2.2 功能 3. 事件 3.1 主要函数 3.2 功能 1. 概述 GKI以库libbt-brcm_gki.so(Static Lib?)的形式提供给 ...
- NRF51822之GPIOTE介绍
Note This library is obsolete and should not be used in new designs. Instead, you should use GPIOTE ...