如何在Qt应用程序中通过终端命令运行分离的应用程序? [英] How to run a detached application by terminal command in Qt application?

查看:411
本文介绍了如何在Qt应用程序中通过终端命令运行分离的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用命令:

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

在Qt应用程序中的button的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"); 
}

我添加了必要的库以使用它.但是运行应用程序时出现错误.

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

我该如何解决?如果我做的方法不正确,请给我另一种方法.预先谢谢你!

解决方案

我认为您在这里有两个问题:

首先,您的QProcess process2可能会在完成之前超出范围(即,由于超出范围而被销毁).您要么必须等待它完成(使用waitForFinished(),要么使其成为指针或成员变量(以更改范围),然后将finished()信号连接到某个处理插槽(可以进行整理). /p>

这里的另一件事是,看起来您只想设置工作目录,所以我不认为将cd命令管道到您的可执行文件中是可行的方法,这样做会更容易: /p>

编辑

我已经编辑了示例,向您展示了如何获取输出:

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();

我在大约2分钟的时间内把它敲在了Windows盒子上,并为您进行了测试...我可以在linux上完成此操作,但这将花费我一点时间,因为我必须将其启动:o ...但是如果你愿意的话.

编辑2

如果要完全分离该过程:

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");

现在我不是100%确定您可以从过程中获取输出...现在与您的Qt应用无关...

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!

解决方案

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...

这篇关于如何在Qt应用程序中通过终端命令运行分离的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆