Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Wait for Qprocess to finish

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!

like image 403
Mohammad Reza Ramezani Avatar asked Jul 31 '14 14:07

Mohammad Reza Ramezani


2 Answers

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();
like image 119
ahmed Avatar answered Oct 13 '22 17:10

ahmed


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";
like image 25
TheDarkKnight Avatar answered Oct 13 '22 18:10

TheDarkKnight