March 15, 2012

kitten kitten
Lab Rat
163 posts

using QProcess[solved]

 

hi to everyone
i want to run an application in my program so i use qprocess,and i use this code:

  1.  QString program = "calc.exe"
  2. QProcess process(this);
  3.  process.start(program);
  4.  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?

 Signature 

www.kitten.mihanblog.com

4 replies

March 15, 2012

mlong mlong
Robot Herder
1551 posts

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:

  1. QString program = "calc.exe";
  2. QProcess *process = new QProcess(this);  // On the heap, not the stack.
  3.  
  4. // Also, connect your signals and slots /before/ you start your process.
  5. // You can also eliminate the "this", and use the 3-parameter version of
  6. //      connect() which assumes "this" as the owner of the slot.
  7. connect(process, SIGNAL(error(QProcess:ProcessError)),
  8.         SLOT(processError(QProcess::ProcessError)));  
  9.  
  10. process->start();  // Then start your process once everything is all set up.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

March 16, 2012

Jeroentje@home Jeroentje@ho..
Dinosaur Breeder
731 posts

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.

 Signature 

Greetz, Jeroen

March 16, 2012

mlong mlong
Robot Herder
1551 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

March 17, 2012

kitten kitten
Lab Rat
163 posts

i do everything mlong said, and it works fine for me now,and my problem solved ,thanks

 Signature 

www.kitten.mihanblog.com

 
  ‹‹ enable shared cache in sql server 2008      QLibrary and QtCreator’s Utils plugin ››

You must log in to post a reply. Not a member yet? Register here!