我正在尝试在Qt中打开文件。我在此功能中使用了双反斜杠
doc->dynamicCall("Open(QVariant)", "E:\\QT\\build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\\My Question.doc")
而且有效。但是,我的目录变量的绝对路径返回此值,并带有正斜杠:
"E:/QT/build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug/My Question.doc"
而且它不起作用。错误说:“抱歉,我们找不到您的文件。是否可能将其移动,重命名或删除了?”我尝试了所有方法,但只能在双反斜杠中使用。
我知道我必须将
\
转义为\\
,但是如何使用变量编写此函数? 最佳答案
我认为toNativeSeparators
函数可能会对您有所帮助。由于您使用的是Windows,它将把正斜杠替换为反斜杠。
代码是这样的:
string path = "E:/QT/build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug/My Question.doc";
doc->dynamicCall("Open(QVariant)", QDir::toNativeSeparators(path));