本文介绍了如何在 Windows 上将 Qt QString 转换为 LPCTSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开注册表并修改它.这是我打开注册表的方式:

I tried to open registry and modify it. This is how I open the registry:

HKEY hKey;
LPCTSTR subKey = TEXT("a registry subkey to be opened");
RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS , &hKey);

但这里有一个问题,我想使用 QString 来务实地更改子键.并把 QString 像这样:

But here is a problem, I want to use a QString to change the subkey pragmatically. And put the QString like this:

QString subKeyString = QString("%1").arg(subKeyName);
LPCTSTR subKey = TEXT(subKeyString); //but it's not working here

我认为是因为我没有将 QString 更改为 LPCTSTR,我尝试了此解决方案,但仍然无法找到一种方法来放置自定义 QStringTEXT 宏中.我不太确定引擎盖下的 WinApi,我只是尝试了我可能做的事情.

有什么办法可以解决这个问题吗?

I thought it's because I did not change the QString to LPCTSTR, I tried this solution, but still I can't figure out a way to put a custom QString into the TEXT macro. I am not quite sure the WinApi under the hood, I just tried what I could possibly do.

Is there a way I can fix this problem?


下面是我如何将 QString 转换为 LPCTSTR:

QString testString = "converting QString to LPCSTR";
QByteArray testStringArr = testString.toLocal8Bit();
LPCSTR lp = LPCSTR(testStringArr.constData()); //the QString is converted to LPCTSTR
//but when I put the LPCSTR to the TEXT macro, the error is still there, like the next line below will not complie
LPCSTR lp = TEXT(LPCSTR(testStringArr.constData())); //This line will not work

推荐答案

TEXT() 宏仅适用于编译时文字,不适用于运行时数据.TCHAR 和相关 API 旨在通过在 char 之间映射文字,帮助人们将他们的代码从基于 ANSI 的 Win9x/ME 迁移到基于 Unicode 的 WinNT 4+wchar_t,以及 AW 变体之间的映射函数名称.但那些日子已经一去不复返了.

The TEXT() macro only works with compile-time literals, not with runtime data. TCHAR and related APIs were designed to help people migrate their code from ANSI-based Win9x/ME to Unicode-based WinNT 4+, by mapping literals between char and wchar_t, and mapping function names between A and W variants. But those days are LONG gone.

在这种情况下正确的解决方案是完全忽略TCHAR而只关注Unicode.QString 是 Unicode 字符串的包装器.所以只使用基于 Unicode 的注册表 API 函数并假装 TCHAR 不存在.

The correct solution in this situation is to ignore TCHAR altogether and focus only on Unicode. A QString is a wrapper for a Unicode string. So use the Unicode-based Registry API functions only and pretend TCHAR doesn't exist.

在 Windows 上,基于 Unicode 的 API 需要 UTF-16 编码的 wchar_t 字符串.使用 QString::toStdWString() 方法得到一个 std::wstring,它是一个 wchar_t 字符串的 C++ 包装器:>

On Windows, Unicode-based APIs expect UTF-16 encoded wchar_t strings. Use the QString::toStdWString() method to get a std::wstring, which is a C++ wrapper for a wchar_t string:

QString subKeyString = QString("%1").arg(subKeyName);
std::wstring subKey = subKeyString.toStdWString();
HKEY hKey;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, subKey.c_str(), 0, KEY_ALL_ACCESS, &hKey);

或者,您可以使用 QString::utf16() 方法.但是,它返回一个 const ushort* 指针,因此您必须将其类型转换为 const wchar_t*:

Alternatively, you can use the QString::utf16() method. However, it returns a const ushort* pointer, so you will have to type-cast it to const wchar_t*:

QString subKeyString = QString("%1").arg(subKeyName);
LPCWSTR subKey = reinterpret_cast<LPCWSTR>(subKeyString.utf16());
HKEY hKey;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS, &hKey);

这篇关于如何在 Windows 上将 Qt QString 转换为 LPCTSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:00