Qt:QProcess调用terminal +脚本 [英] Qt: QProcess to call terminal + script

查看:2081
本文介绍了Qt:QProcess调用terminal +脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用QProcess真正的麻烦,我看过几个地方使用它,但每次我使用它我的程序冻结,或者它只是不做我想要做的。 p>

我想从GUI应用程序中执行以下操作:



将目录更改为/ Users / Tim / etc等等。
从那里我需要调用gnuplot并加载脚本。



在终端窗口中我通常会做以下事情:

 > cd / Users / Tim / ... 
> / opt / local / bin / gnuplot barchartscript.txt

目前我使用系统调用这样做,但是每个人都建议使用QProcess,所以我想这样做。



我的代码看起来现在与QProcess:

  QObject * parent; 
QProcess * process = new QProcess(parent);
QString命令;
QString changed =cd / Users / Tim / etcetc;
commands =(changed +&& / opt / local / bin / gnuplot scatterplotscriptwithout.txt)c_str

process-> start(commands);

任何人都可以告诉我有什么问题?

解决方案

此片段可能是有用的(未测试的)。有关每种方法的详细信息,请参阅 QProcess 文档。

  process-> setWorkingDirectory(/ Users / Tim / etcetc); 
process-> setArguments(QStringList()<<scatterplotscriptwithout.txt);
process-> start(/ opt / local / bin / gnuplot);

EDIT



在你的程序中似乎有错误的另一件事是未定义的指针,你作为父对你的QProcess实例。设置父级以利于自动删除子级是有用的。在这种情况下,只需要自己删除QProcess实例。



EDIT 2(错误处理)

  QProcess * process = new QProcess; 
connect(process,SIGNAL(error(QProcess :: ProcessError)),this,SLOT(handleError(QProcess :: ProcessError)));

然后定义一个正确的插槽(此处为handleError)

  void MyClass :: handleError(QProcess :: ProcessError error){
switch(error){
case QProcess :: FailedToStart:
qDebug )<< 无法启动,可能是由于权限不足;
break;
case QProcess :: Crashed:
qDebug()<< 程序崩溃了。
break;
//调试每个案例..
}
}

有关所有枚举值的详细信息,请参见此处



如果您的QProcess正确结束但没有预期的输出,您可以查看您的进程的退出代码,并参考gnuplot手册页。


I am having real trouble with the use of QProcess, I've looked at several locations to use it, but everytime I use it my program freezes, or it just doesn't do what I want it to do.

What I want to do from my GUI application is the following:

Change directory to /Users/Tim/etc etc. From there I need to call gnuplot and load a script into it.

What I'd normally would do in a terminal window is the following:

 > cd /Users/Tim/...        
 > /opt/local/bin/gnuplot barchartscript.txt

At the moment I'm using system call to do this, and that works, but everybody recommends using QProcess, and so I would like to do that.

How my code looks now with QProcess:

    QObject *parent;
    QProcess *process = new QProcess(parent);
    QString commands;
    QString changed = "cd /Users/Tim/etcetc";
    commands = (changed + "&& /opt/local/bin/gnuplot scatterplotscriptwithout.txt").c_str();

    process->start(commands);

Can anybody tell me what's wrong? Or the correct way to do multiple commands in one process?

解决方案

This snippet may be useful (untested). Refer to QProcess documentation for detail on each method.

process->setWorkingDirectory("/Users/Tim/etcetc");
process->setArguments(QStringList() << "scatterplotscriptwithout.txt");
process->start("/opt/local/bin/gnuplot");

EDIT

Another thing that seem wrong in your program is the undefined pointer that you give as parent to your QProcess instance. It is useful to set a parent to take benefit of an automatic children deletion. In this case, just take care to delete the QProcess instance yourself.

EDIT 2 (error handling)

QProcess *process = new QProcess;
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleError(QProcess::ProcessError)));

Then define a proper slot (here handleError)

void MyClass::handleError(QProcess::ProcessError error) {
   switch(error) {
   case QProcess::FailedToStart:
    qDebug() << "Failed to start, may be due to insufficient permissions";
    break;
    case QProcess::Crashed:
    qDebug() << "Program crashed.";
    break;
    //debug each case..
   }
}

See here for a detail of all the enum values.

If your QProcess ends correctly but not with the expected output, you can look at the exit code of your process and refer to the gnuplot man page for information.

这篇关于Qt:QProcess调用terminal +脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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