简体   繁体   English

使用 QProcess 启动另一个 exe 并生成路径错误的文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM