Run command using QProcess
-
Hello,
I am developing application for windows which can shows the corrupt files in the directory with the use of QProcess. I have opened the file using QProcess but i want to know the exit status of the command means whether the is correctly opened or not using QProcess.
Please help me out. How can i get the finished signal status using QProcess.Regards
Abhinay
S/w engg
Virtual employee pvt ltd. -
Hi, welcome to devnet
You can wait till the process is finished with "waitForFinished()":http://qt-project.org/doc/qt-5/qprocess.html#waitForFinished and then check the return code with "exitCode()":http://qt-project.org/doc/qt-5/qprocess.html#exitCode
-
Hello Chrls,
I have done this but every time when i am checking the exit status, the exit status is always normal whether the file is corrupted or not. I want If the file is corrupted and if the file is not properly opened then my code should return false.
I am just sending you the code as i have written.///////////////////////////////////////////////////////////////////////////////////////
QString StrValue = m_AllFileList.at(i);QProcess *p = new QProcess(this);
p->start((const char*) StrValue.toAscii());
p->waitForFinished();
p->start("cmd.exe", QStringList() << "/C" << (const char*)
StrValue.toAscii());QProcess::ExitStatus Status = p->exitStatus(); if (Status == 0)
{
cout<<" File is corrupted " ;
}else { cout<<" File is OK " }
-
QString StrValue = m_AllFileList.at(i);
QProcess *p = new QProcess(this);
p->start((const char*) StrValue.toAscii());
p->waitForFinished();
p->start(“cmd.exe”, QStringList() << “/C” << (const char*) StrValue.toAscii());
QProcess::ExitStatus Status = p->exitStatus();
if (Status == 0) { cout<<” File is corrupted “ ; }
else { cout<<” File is OK “ }
-
Please use code tags when posting code (first button to the right).
This doesn't look right. What is the actual process you start and does it have meaningful return codes? Why are you starting the process twice? One directly and second via cmd shell? The second time you don't even wait till it's finished before checking the status.
The exitStatus() only reports if the process succeeded or crashed. You shouldn't compare it to 0. It can have two possible values: QProcess::NormalExit(which happens to be 0 now but is not guaranteed to stay so in the future) and QProcess::CrashExit.
If you want to get the return code from the process use exitCode(), not exitStatus().
-
@QProcess *p = new QProcess(this);
p->start("cmd.exe", QStringList() << "/C" << (const char*) StrValue.toAscii());
p->waitForFinished();
QProcess::ExitStatus Status = p->exitStatus();
if (Status == 0) { cout<<” File is corrupted “ ; }
else { cout<<” File is OK “; } @
-
Sorry for the previous one.
The last code is the actual code.Thanks
Abhinay Chauhan -
Again, exitStatus() returns an enum with two values, not an int (0).
The value of QProcess::NormalExit(what you get) means only that the process finished without crashing. Depending on what your process is it will never crash (or at least shouldn't if you did it right).If your process returns some sort of code like 0 for ok and 1 for corrupted files then check exitCode(), not exitStatus().
Btw. don't convert the param to const char*. You are putting it into a QStringList so it will be converted back to QString anyway.
-
I have updated the code but every time the (p->exitCode() == 0) whether the file is corrupted or the file is correct.
@QString StrValue = m_AllFileList.at(i);
QProcess *p = new QProcess(this);
p->start("cmd.exe", QStringList() << "/C" << StrValue.toAscii());
p->waitForFinished();if (p->exitCode() == 0)
{
int temp = 0;
}else
{
int temp = 0;
}@ -
@for (int i = 0; i < m_AllFileList.size(); ++i)
{
QString StrValue = m_AllFileList.at(i);QProcess *p = new QProcess(this);
p->start("cmd.exe", QStringList() << "/C" << StrValue.toAscii());
p->waitForFinished();if (p->exitCode() == 0)
{
int temp = 0;
}else
{
int temp = 0;
}
}
@ -
What exactly does m_AllFileList contain?
I hope you're not doing something like "cmd /C file.txt" because this would make no sense at all. -
m_AllFileList is a list that contains the Path of the Audio, video and image files in the directory.
so i want to run the command on the files through QProcess to get the corrupt files in the directory.
for example - If m_AllFileList contains a image file path and we are going to open the same image file but there is not preview showing and the window photo viewer shows the dialog "Windows Photo viewer can't open this picture because ...."
so i want to get these files which are not opened by the Windows photo viewer, Windows Media Player for image and Audio video files respectively.