Qt - Wait for Qprocess to finish

别等时光非礼了梦想. 提交于 2020-01-01 03:14:23

问题


I'm using CMD by QProcess but I have a problem.

My code:

QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.waitForFinished();
process.close();

When I don't pass an argument for waitForFinished() it waits for 30 secs. I want to terminate QProcess after CMD command is executed! Not much and not less!


回答1:


You need to terminate the cmd.exe by sending exit command, otherwise it will wait for commands Here is my suggestion:

QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.write ("exit\n\r");
process.waitForFinished();
process.close();



回答2:


The process you're starting is cmd.exe which, by itself will, not terminate. If you call cmd with arguments, you should achieve what you want: -

QProcess process;
process.start("cmd.exe \"del f:\\b.txt"\"");
process.waitForFinished();
process.close();

Note that the arguments are escaped in quotes.

Alternatively, you could call the del process, without cmd: -

QProcess process;
process.start("del \"f:\\b.txt"\"");
process.waitForFinished();
process.close();

Finally, if you just want to delete a file, you could use the QFile::remove function.

QFile file("f:\\b.txt");
if(file.remove())
    qDebug() << "File removed successfully";


来源:https://stackoverflow.com/questions/25061943/qt-wait-for-qprocess-to-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!