Using QProcess[solved]
-
hi to everyone
i want to run an application in my program so i use qprocess,and i use this code:
@ QString program = "calc.exe"
QProcess process(this);
process.start(program);
connect( &process, SIGNAL(error(QProcess::ProcessError)),this,SLOT(processError(QProcess::ProcessError)));
@
and so i will receive its errors,it will apear for one second and after that it crashed and the error say it is crashed
i want to know why?and am i write this program correctly? -
You're creating your QProcess on the stack, but you're giving it a parent. That's a bad thing that you must not do.
Try this:
@
QString program = "calc.exe";
QProcess *process = new QProcess(this); // On the heap, not the stack.// Also, connect your signals and slots /before/ you start your process.
// You can also eliminate the "this", and use the 3-parameter version of
// connect() which assumes "this" as the owner of the slot.
connect(process, SIGNAL(error(QProcess:ProcessError)),
SLOT(processError(QProcess::ProcessError)));process->start(); // Then start your process once everything is all set up.
@ -
Hi,
I've done some QProcess stuff myself, but to know why your program crashed is not so easy I think. The QProcess just keeps track of your "client" program. The "client" program itself will run on the OS. It isn't set within the HEAP of your Qt program! The QProcess just get's the information back from the OS about that "client" process you started. What mLong said is in dead a big improvement. First connect your signals, then start the process. The process might even crash when the start command is entered.
The value you get from the client when it crashed has probably to do with OS values. The same as old DOS programs always exiting with 1 when all went ok.
Btw, when you kill your own program and do not use the process->close() the "client" program will keep on running.
If you really want to know why the client crashed, you may need to look into your OS. For example I needed to control the Window in which the client was started, but I needed Windows API calls to do so. Just wanted to make sure the Window was raised or when already active it was raised again.Hope this helps a bit.
-
From the look of his code, I'm assuming that it was his program that was crashing, not the client app. (See the note about using this with automatic variables.) More than likely the above code was in a method that was called, the QProcess was instantiated (and unfortunately parented), and then as soon as the method was over, went out of scope and was deleted. But since it was a child of this, then all kinds of nastiness happened.