相对文件路径的差异:Qt Creator的调试模式和发布模式 [英] Difference in relative file path: debug mode and release mode of Qt Creator

查看:195
本文介绍了相对文件路径的差异:Qt Creator的调试模式和发布模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 QFile file("test.txt");
 if (file.open(QIODevice::ReadOnly)) {
     qDebug()<<"You got me.";
 }


我正在使用:


I am using:

  • 带有MSVC 2010的Qt 4.8.6
  • Qt Creator 3.1.1
  • Windows 7(32位)

根据上面的代码,如果未更改.pro文件,则为相应的构建目录

From the above code, if .pro file has not been changed, the corresponding build directory

用于调试模式:

D:\...\build-Main-MSVC2010-Debug

和调试模式的.exe将位于

D:\...\build-Main-MSVC2010-Debug\debug

用于发布模式:

D:\...\build-Main-MSVC2010-Release

和释放模式的.exe将位于

D:\...\build-Main-MSVC2010-Release\release


[问题]

如果我要发布程序阅读"test.txt",文件,我将文件放在文件夹中


[Question]

If I want the release program to read the "test.txt" file, I put the file in the folder

D:\...\build-Main-MSVC2010-Release\release

这很有意义.

但是,如果我想让调试程序读取"test.txt",文件,我必须将文件放在文件夹中

But if I want the debug program to read the "test.txt" file, I have to put the file in the folder

D:\...\build-Main-MSVC2010-Debug\

而不是

D:\...\build-Main-MSVC2010-Debug\debug

我想知道为什么相对文件路径在debug&发布模式,已经困扰了我很长时间了.

I am wondering why the relative file path worked differently in debug & release mode, it has been bothering me for a long time.

感谢@Paul和@lpapp.这是工作目录的屏幕截图:

Thanks for @Paul and @lpapp. Here is the screenshot of working directory:

调试:

发布:

对于@Paul和@lpapp:

For @Paul and @lpapp:

我曾经将所需的.dll复制到release文件夹以测试运行时,并且我发现如果我通过Qt Creator执行发布程序,则工作目录将像您所说的那样工作.但是,如果我直接单击发布文件夹中的.exe,情况将与我在问题中所说的一样.我认为从Qt Creator运行程序和直接执行程序之间可能会有差异.

I used to copy required .dll to the release folder to test runtime, and I just found if I execute the release program through Qt Creator, the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question. I think there might be differences between running program from Qt Creator and directly executing the program.

推荐答案

这取决于您程序的当前工作目录.您可以在Project-> Run settings-> Run-> Working目录中更改它.

This depends on current working directory of your program. You can change it in Project->Run settings->Run->Working directory.

如果我通过Qt Creator执行发布程序,则工作目录将像您所说的那样工作.但是,如果我直接单击发布文件夹中的.exe,情况将与我在问题中所说的一样.

if I execute the release program through Qt Creator, the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question.

这是因为,当您在发行文件夹中单击.exe时,该文件夹是该程序的工作目录.从Qt Creator运行程序时,Qt Creator显式设置工作目录.您可以将工作目录覆盖为创建.exe文件的目录,因此无论您是从Qt Creator运行程序还是在资源管理器中单击.exe都没有区别.

This is because when you click the .exe in the release foder, that folder is the working directory for the program. When you run the program from Qt Creator, Qt Creator sets the working directory explicitly. You may override the Working directory to be the directory where .exe file is created so it'll be no difference whether you run the program from Qt Creator or just click the .exe in Explorer.

这篇关于相对文件路径的差异:Qt Creator的调试模式和发布模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆