【发布时间】:2023-04-09 17:09:02
【问题描述】:
我试图打开注册表并修改它。这就是我打开注册表的方式:
HKEY hKey;
LPCTSTR subKey = TEXT("a registry subkey to be opened");
RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS , &hKey);
但是这里有一个问题,我想使用QString 务实地更改子键。并把QString 像这样:
QString subKeyString = QString("%1").arg(subKeyName);
LPCTSTR subKey = TEXT(subKeyString); //but it's not working here
我以为是因为我没有将QString 更改为LPCTSTR,我尝试了这个解决方案,但我仍然无法找到将自定义QString 放入TEXT 宏的方法。我不太确定引擎盖下的 WinApi,我只是尝试了我可能做的事情。
有什么办法可以解决这个问题吗?
编辑:
这是我将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
【问题讨论】:
-
@infixed 我确实看到了一些类似的解决方案,但我认为只需将
QString转换为LPCTSTR,我的问题仍然存在。我将编辑我的问题,以便您更清楚地知道我遇到了什么错误。 -
仅供参考,QSettings 类提供了 Qt 和 Windows 注册表之间的一些集成,尽管它可能适用于您的情况。
-
@walkingTarget 我尝试使用
QSettings将注册表修改为我需要的,但不工作,除了我使用本机WinApi。所以当我使用windows api时,我遇到了这个问题。我编辑了我的问题,有什么解决方案吗? -
TEXT就像QStringLiteral... 在相同的情况下使用:当您传入字符串时。