Solved QProcess and dd under linux
-
Hi,
I am trying without success to launch dd to copy one file with QProcess under linux.I want to wait that my dd command is finished.I have tried without successss with QProcess::execute and with Qprocess::startdetached (static functions). I don't know if I need a "/bin/sh -c" before my dd command or if I don't need "/bin/sh -c".the copy with dd doesn't happen.(This code that call QProcess and dd is in a secondary thread).I have tried with and without "/bin/sh -c" before dd.my dd command is like "dd if=source file of=destination file".example code without /bin/sh -c :
QString cmd; int res2; cmd=QString("dd if=")+strFileIn+QString(" of=")+strFileOut; res2=QProcess::execute(cmd);
or
with /bin/sh -c :qint64 pid; QString arg0("-c"); QString arg1("if="); arg1+=strFileIn; QString arg2(" of="); arg2+=strFileOut; arg2+="\""; QString arg3("\"dd "); arg3+=arg1+arg2; QStringList list; list+=arg0; list+=arg3; //list+=arg2; int status; bool res3=QProcess::startDetached("/bin/sh",list,QString(),&pid); waitpid(pid,&status,0);
Has someone any good idea ?
-
Hi
You need sh -c if external command/exe file - afaik
you should check with "man sh"There is waitForFinished to wait for completion.
Try the ping example
QProcess pingProcess;
QString exec = "ping";
QStringList params;
params << "-c" << "1" << IP;
pingProcess.start(exec,params,QIODevice::ReadOnly);
pingProcess.waitForFinished(-1);
QString p_stdout = pingProcess.readAllStandardOutput();
QString p_stderr = pingProcess.readAllStandardError();
qDebug() << p_stdout ;
qDebug() << p_stderr ; -
@mrjj Hi,
Ok I have debugged to find the problem and I have this error :Object: Cannot create children for a parent that is in a different thread.
(Parent is TaskThread(0x1c08840), parent's thread is QThread(0x14449a0), current thread is TaskThread(0x1c08840)the function that call this QProcess code is in the secondary thread "TaskThread". It seems it is not possible to call a method of QProcess like start or execute in this secondary thread (I am with Qt 5.6.0 under linux)
do you have any further idea ? -
@stephane78 You should show how you start that second thread and what exactly you're doing there.
Is there a reason why you use a second thread? -
Hi
well it must be done differently, then it should workhttps://wiki.qt.io/Threads_Events_QObjects
So maybe you just need to send a signal to thread and slot will start QProc.
-
@mrjj,jsulm,Hi again, I have a solution with system but as I have corrected two other errors I will try again with QProcess so I am not sure that the secondary thread was the error.so my first error : I had forgotten to create the destination path of the destination file (the subdirectory) so it made an error and the second error: I should have removed the character '\' of the source and destination file strings and put " before and after the two string of file (if="source" and of="destination".
with these two errors corrected it runs well with system(cmd) and I will try again now with QProcess and come back to report here if it works... -
so it works too with QProcess::execute(cmd).
As I said previously I removed the character '\' that is in the strings source and destination before the spaces.I must still think about the accentued characters in the source and destination
files.but my problem is solved....