【发布时间】:2020-05-12 21:29:13
【问题描述】:
我试图在我的 gui 上推送 QPushButton 后立即使用 QProcess 执行命令行。
问题我的问题是.sh 可执行文件永远不会执行。
我尝试执行的脚本非常简单,报告如下:
#!/bin/bash
echo "try one two three"
rostopic echo -b LaserScan_PointCloud2_test.bag -p /scan > laserScan_test_1.csv
在激活按钮的函数下方:
filterpcdinterface.h
private slots:
void on_executeScriptBtn_clicked();
private:
QProcess *executeBash;
filterpcdinterface.cpp
FilterPCDInterface::FilterPCDInterface(QNode *node, QWidget *parent) :
qnode(node),
QMainWindow(parent),
ui(new Ui::FilterPCDInterface)
{
ui->setupUi(this);
executeBash = new QProcess;
executeBash->setProcessChannelMode(QProcess::MergedChannels);
connect(executeBash, &QProcess::readyReadStandardOutput, [this] {
qDebug() << "This is the output from the process: ";
on_executeScriptBtn_clicked();
});
}
void FilterPCDInterface::on_executeScriptBtn_clicked()
{
executeBash->waitForFinished();
QString script("/home/emanuele/Desktop/bags/test.sh");
executeBash->start("sh",QStringList() << script);
if(!executeBash->waitForStarted()) //default wait time 30 sec
qWarning() << " cannot start process ";
int waitTime = 60000 ; //60 sec
if (!executeBash->waitForFinished(waitTime))
qWarning() << "timeout .. ";
executeBash->setProcessChannelMode(QProcess::MergedChannels);
QString str(executeBash->readAllStandardOutput());
}
到目前为止,我已经查阅了几篇文章,但没有一篇文章能帮助我解决问题。 我遇到了this one 和this one,我实际上是从那里得到这个想法的。
作为解释器,我尝试了"/bin/sh" 和"sh",但它们都没有给出预期的结果。
准确地说,我尝试了这两个:
executeBash->start("sh",QStringList() << script);
和
executeBash->start("/bin/sh",QStringList() << script);
但什么也没发生。
我终于看到了this 非常有用的帖子,它实际上帮助我设置了整个按钮功能,但是到了执行脚本的时候,这次也没有任何反应。
我不确定这种奇怪的行为是否是由构造函数中的connect 函数引起的。问题还在于qDebug() 语句也永远不会到达。
official documentation 提到了使用startDetached 语句的可能性,但我不确定它是否与我想要实现的目标完全相关。
官方文档总是报告以下声明here
Unix:启动的进程将在自己的会话中运行,并像一个 守护进程。
因此我认为有一个进程会话正在工作并且可以执行但它不是。
总结:我一直在研究问题可能是什么,但我一直错过一些我看不到的东西。请指出正确的方向以帮助解决此问题,是否有人碰巧遇到同样的问题。
【问题讨论】:
-
你怎么知道脚本没有执行?因为标准输出为空是正常的
-
您好@thibsc,感谢您阅读这个问题。我知道这是因为声明
qDebug() << "This is the output from the process: "从未在终端上显示。因此文件的执行没有发生,我不知道为什么。 -
这可能是正常的,因为您将输出重定向到一个文件中,尝试在脚本中添加一个回显,看看是否仍然没有任何反应
-
好的,我试了一下并更新了问题。基本上我在 bash 文件中添加了
echo "try one two three",但仍然没有任何反应 -
您阅读了
on_executeScriptBtn_clicked()末尾的标准输出,您打印了strvar 的内容以查看它是否包含某些内容?