3

I want to use commands:

cd /opencv/opencv-3.0.0-alpha/samples/cpp/
./cpp-example-facedetect lena.jpg

to run a sample code of OpenCV on clicked() method of button in Qt application. So I use:

void MainWindow::on_btSample_clicked()
{
        QProcess process1;
        QProcess process2;

        process1.setStandardOutputProcess(&process2);

        process1.start("cd /opencv/opencv-3.0.0-alpha/samples/cpp");
        process1.waitForBytesWritten();
        process2.start("./cpp-example-facedetect lena.jpg"); 
}

I added necessary library to use it. But I have an error when I run my application.

QProcess: Destroyed while process ("./cpp-example-facedetect") is still running.

How can I fix it? If the way I make isn't right, plz give me another way. Thank u in advance!

My Will
  • 400
  • 4
  • 27

1 Answers1

3

I think you have two issues here:

Firstly your QProcess process2 is probably going out of scope before it finishes (i.e. gets destroyed since its out of scope). You either have to wait for it to finish (using waitForFinished(), or make it a pointer or member variable (to change the scope) and connect the finished() signal to some handling slot (which can do the tidy up).

The other thing here is, it looks like you just want to set the working directory, so I don't think piping the cd command into your executable is the way to go, it would be easier to do something like:

EDIT

I have edited my example to show you how to get the output:

QProcess myProc;

qDebug() << "Starting process\n";
// Setup the working directory
QDir::setCurrent("D:\\software\\qtTest");

// Start the process (uses new working dir)
myProc.start("test.bat");

myProc.waitForFinished();
qDebug() << myProc.readAll();

I knocked this up on my windows box in about 2 minutes and tested it for you... I could do it on linux but that will take me a little longer because I have to boot it up :o ... but if you want I will.

EDIT 2

If you want to detach the process entirely:

QProcess myProc;

qDebug() << "Starting process\n";
// Setup the working directory
QDir::setCurrent("D:\\software\\qtTest");

// Start the process (uses new working dir)
myProc.startDetached("test.bat");

Now I am not 100% sure you can get the output back from the process... it is now nothing to do with your Qt app...

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • i tried this but I got nothing, there is no error and no result in screen. Have u another way to make it run? Thank u – My Will Apr 13 '16 at 09:42
  • I have edited my example... I think it is working for you, but you don't see anything maybe because you are not retrieving the output?... so use waitForFinished() and then read the output using `readAll()`. IF this is a long process then you probably need to set it up differently and wait for the readReady() signal and read output as it comes in ... again using slot/signal. If you need that example will take longer to write it. – code_fodder Apr 13 '16 at 09:57
  • thank u very much. the sample code run as a widget application, it'll show an image, but I don't know the retrieving is necessary in my case. What I need is just run it and don't care its result :D. – My Will Apr 13 '16 at 10:07
  • oh, but currently my app don't show anything when I click the button :(. It's not the thing I expect :( – My Will Apr 13 '16 at 10:17
  • Oh sorry... what do you mean? What do you want it to show? I am not 100% clear. The code I wrote will only send the text to debug... not to the GUI – code_fodder Apr 13 '16 at 10:19
  • what I mean is I need run the sample application seperately. It doesn't associate my application. and, additional info about sample application is: ".exe" in the end of its name. I don't know if it's particular case? sorry for my poor english :D. my explaination is so bad :D – My Will Apr 13 '16 at 10:26
  • hmm... I `think` I understand what you mean... take a look at `startDetached()`, this will start the application in a new process, but it will detatch it from your application. So... if you close your Qt app `cpp-example-facedetect` will keep running... let me try to re-jig my example again. – code_fodder Apr 13 '16 at 10:33
  • oh, definitely. Bother u again, so sorry my friend :D – My Will Apr 13 '16 at 10:38
  • So.... I added the example, its actually simpler. When I run this, now I see that a separate command prompt starts (in your case some other GUI?) and I close my Qt app and its still running. My bat file is just pinging www.google.com continuously :o – code_fodder Apr 13 '16 at 10:41
  • I found my problem. Thank u very much :) – My Will Apr 13 '16 at 10:50
  • Cool : ) ... remember if it was something different to what is in my answer you should clarify it so that the next person can read about it : ) – code_fodder Apr 13 '16 at 11:20