参考资料:

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:

注: 关于UNICODE宏的设置

  1. #include <iostream>
  2.  
  3. #include <Windows.h>
  4. #include <QtCore/QtCore>
  5.  
  6. #ifdef UNICODE
  7. #define FILE_NAME "数据.txt"
  8. #else
  9. #define FILE_NAME "data.txt"
  10. #endif
  11.  
  12. int main()
  13. {
  14. QString szqFileName = QString::fromLocal8Bit(FILE_NAME);
  15. qint64 nLen = szqFileName.length();
  16. //
  17. // QString to TCHAR*
  18. //
  19. LPCTSTR szFileName = NULL;
  20. #ifdef UNICODE
  21. szFileName = (wchar_t*)szqFileName.utf16();
  22. #else
  23. QByteArray byteArray = szqFileName.toLocal8Bit();
  24. szFileName = byteArray.constData();
  25. #endif // UNICODE
  26. //
  27. // Create file
  28. //
  29. HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, , NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  30. if (hFile == INVALID_HANDLE_VALUE)
  31. std::cout << "Cannot create file." << std::endl;
  32. else
  33. std::cout << "File created." << std::endl;
  34. ::CloseHandle(hFile);
  35. //
  36. // TCHAR* to QString
  37. //
  38. szFileName = TEXT(FILE_NAME);
  39. #ifdef UNICODE
  40. szqFileName = QString::fromUtf16((ushort*)szFileName);
  41. #else
  42. szqFileName = QString::fromLocal8Bit(szFileName);
  43. #endif
  44. QFile file(szqFileName);
  45. if (!file.remove())
  46. std::cout << "Cannot remove file." << std::endl;
  47. else
  48. std::cout << "File removed." << std::endl;
  49.  
  50. return ;
  51. }

QString 和 TCHAR 的相互转换的更多相关文章

  1. 【转】QT QString, wchar_t *, TCHAR, CString和其他字符或字符串类型的转化

    //QString to wchar_t *: const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileNa ...

  2. QT5中QString与char *的相互转换

    以例子说明: #include <QApplication> #include <QDebug> #include <QString> #include <Q ...

  3. QString与char*的相互转换

    原地址:http://blog.sina.com.cn/s/blog_5c70dfc80100r0nh.html 一.QString转char*   QString str; int num=0; s ...

  4. QString与string的相互转换

    1.QString转换String string s = qstr.toStdString(); 2.String转换QString QString qstr2 = QString::fromStdS ...

  5. QString与string的相互转换【转载】

    文章转载自https://blog.csdn.net/qq_33485434/article/details/80680506 1.QString转换String string s = qstr.to ...

  6. Qt QString 和 LPCWSTR 的相互转换

    在windosw 编程中,常用到LPCWSTR 变量,QT中最常用到QString,下面提供QString和LPCWSTR 相互转换的方法 LPWSTR 转换成QString LPCWSTR str; ...

  7. QString, string, int, char* 之间相互转换

    这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...

  8. 【原创】<Debug> QString

    [问题1] 'class QString' has no member named 'toAscii' [解答] 把toAscii().data()改成toLatin1().data() 如果QStr ...

  9. QString转换为LPTSTR(使用了reinterpret_cast,真是叹为观止,但是也开阔了思路),三篇文章合起来的各种转换方法

    醉了,windows下宏定义了很多char类型 LPTSTR .今天,直接使用,qt报错,真TM费事. 将“CPU”转化为wcha_t * QString str = "CPU"; ...

随机推荐

  1. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  2. BOM表生成

  3. 1st-code-review summary

    每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...

  4. Cacti安装详细步骤

    原文链接: https://www.centos.bz/2012/01/cacti-install-tutorials/ Cacti-监控MySQL: http://www.cszhi.com/201 ...

  5. 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 ...

  6. Nginx return 关键字配置小技巧

    Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...

  7. 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 ...

  8. C# .Net实现URL绝对路径和相对路径之间互相转换

    网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...

  9. BlueDroid代码分析之GKI

    目录 1. 概述 2. 线程 2.1 主要函数 2.2 功能 3. 事件 3.1 主要函数 3.2 功能 1. 概述 GKI以库libbt-brcm_gki.so(Static Lib?)的形式提供给 ...

  10. NRF51822之GPIOTE介绍

    Note This library is obsolete and should not be used in new designs. Instead, you should use GPIOTE ...