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:
在Qt应用程序中的clicked()按钮方法上运行OpenCV的示例代码。所以我使用:
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!
我该如何解决?如果我的方式不对,请给我另一种方式。提前谢谢你!
1 个解决方案
#1
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).
首先,你的QProcess进程2可能在它完成之前超出范围(即因为超出范围而被销毁)。您必须等待它完成(使用waitForFinished(),或使其成为指针或成员变量(以更改范围)并将finished()信号连接到某个处理槽(可以进行整理)。
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:
另一件事是,看起来你只想设置工作目录,所以我不认为将cd命令输入你的可执行文件是可行的方法,它会更容易做类似的事情:
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.
我在大约2分钟内把它撞到我的窗户盒上并为你测试了......我可以在linux上做到这一点但是这会花费我一点时间,因为我必须启动它:o ...但如果你想要我会。
EDIT 2
编辑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...
现在我不是100%确定你可以从流程中获得输出...现在它与你的Qt应用程序无关......
#1
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).
首先,你的QProcess进程2可能在它完成之前超出范围(即因为超出范围而被销毁)。您必须等待它完成(使用waitForFinished(),或使其成为指针或成员变量(以更改范围)并将finished()信号连接到某个处理槽(可以进行整理)。
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:
另一件事是,看起来你只想设置工作目录,所以我不认为将cd命令输入你的可执行文件是可行的方法,它会更容易做类似的事情:
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.
我在大约2分钟内把它撞到我的窗户盒上并为你测试了......我可以在linux上做到这一点但是这会花费我一点时间,因为我必须启动它:o ...但如果你想要我会。
EDIT 2
编辑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...
现在我不是100%确定你可以从流程中获得输出...现在它与你的Qt应用程序无关......