qprocess qt 打开word_Qt:QProcess调用terminal +脚本

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值