[英]Use QProcess to Start Another exe and Generated file with Wrong Path
I met a question recently.我最近遇到一个问题。 I use
QProcess
class to start B.exe
in A.pro
.我使用
QProcess
class 在A.pro
B.exe
The absolutely path of A.pro
is C:/QT/A/A.pro
. A.pro 的绝对路径是
A.pro
C:/QT/A/A.pro
。 I use windeployqt
to package B.exe
, and put the package folder in C:/QT/A/B/B.exe
.我使用
windeployqt
到 package B.exe
,并将 package 文件夹放在C:/QT/A/B/B.exe
。
In B.exe
, the program will generate a file named test.json
.在
B.exe
中,程序将生成一个名为test.json
的文件。 The test.json
's path is the same as B.exe
. test.json
的路径与B.exe
相同。 But I use follows codes in A.pro
, the test.json
is generated to the path the same as A.pro
but not in folder C:/QT/A/B
:但是我在
A.pro
test.json
到与A.pro
相同的路径,但不在文件夹C:/QT/A/B
中:
bool f = QProcess::startDetached("./B/B.exe");
The line code return true
.行代码返回
true
。 I think test.json
should generated in folder C:/QT/A/B
but not in C:/QT/A
.我认为
test.json
应该在文件夹C:/QT/A/B
中生成,但不在C:/QT/A
中。 When I double click B.exe
standlone, it can generate correctly.当我单独双击
B.exe
时,它可以正确生成。 But when I debug with code above in A.pro
, it works failed and it just created a empty file.但是当我在
A.pro
中使用上面的代码进行调试时,它工作失败,它只是创建了一个空文件。
Can you help me?你能帮助我吗?
If you look into QProcess::startDetached , it reads as follows:如果您查看QProcess::startDetached ,它的内容如下:
The process will be started in the directory workingDirectory.
该过程将在目录workingDirectory 中启动。 If workingDirectory is empty, the working directory is inherited from the calling process.
如果workingDirectory 为空,则工作目录继承自调用进程。
You need to set working directory for QProcess
.您需要为
QProcess
设置工作目录。 When you run B.exe by "double clicking" it.当您通过“双击”运行 B.exe 时。 Your OS sets the working dir from where it was started, ie,
C:/QT/A/B
.您的操作系统从它开始的位置设置工作目录,即
C:/QT/A/B
。 But when you start it from A.exe, its working directory is C:/QT/A/
但是当你从 A.exe 启动它时,它的工作目录是
C:/QT/A/
You can read about QProcess::setWorkingDirectory here .您可以在此处阅读有关 QProcess::setWorkingDirectory 的信息。 But since, you are running it with QProcess::startDetached overload, you just need to pass wanted working directory as a parameter:
但是,由于您使用 QProcess::startDetached 重载运行它,您只需将想要的工作目录作为参数传递:
bool f = QProcess::startDetached("./B/B.exe", QStringList(), "./B/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.