简体   繁体   English

如何在 Qt5 中执行多个 QProcess

[英]How to execute multiple QProcess in Qt5

I have been struggling on how to execute multiple processes using QProcess .我一直在努力研究如何使用QProcess执行多个进程。 I was able to execute a single process, ei opening a terminal window QProcess *openTerminal = new QProcess(this);我能够执行单个进程,即打开一个终端 window QProcess *openTerminal = new QProcess(this); , after that I wanted to execute additional processes but I could not find a way to do that. ,之后我想执行其他进程,但我找不到这样做的方法。 The order of operations I am trying to achieve are:我试图实现的操作顺序是:

1) opening a gnome-terminal (this is solved and am able to open after QPushButton click ) 1)打开一个gnome-terminal(这已经解决并且可以在QPushButton click后打开)

2) navigate to cd ~ 2) 导航到cd ~

3) Go to preferred directory cd catkin_docking_ws/ 3) Go 到首选目录cd catkin_docking_ws/

4) Launch proper application roslaunch lidar_deck lidar_deck_rosbag.launch & 4) 启动适当的应用程序roslaunch lidar_deck lidar_deck_rosbag.launch &

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    startLidar();
}

void MainWindow::startLidar()
{
    // Execution of the QProcess to make sure Lidar App Launcher opens:
    this->executeROSLidarApp = new QProcess(this);
    this->executeROSLidarApp->setProcessChannelMode(QProcess::MergedChannels);
    connect(this->executeROSLidarApp, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            [script = this->executeROSLidarApp](int exitCode, QProcess::ExitStatus exitStatus){
            qDebug() << "[EXEC] FINISHED: " << exitCode << exitStatus;
            if(script->bytesAvailable() > 0) qDebug() << "[EXEC] buffered DATA:" << script->readAll();
    });
    connect(this->executeROSLidarApp, &QProcess::errorOccurred, [script = this->executeROSLidarApp](QProcess::ProcessError error) {
            qDebug() << "[EXEC] error on execution: " << error << script->errorString();
    });
}


void MainWindow::on_launchLidarROSBtn_clicked()
{
    qDebug() << "Launching LIDAR APP";
    QProcess *openTerminal = new QProcess(this);
    QProcess *cd = new QProcess(this);
    QProcess *cd_catkin = new QProcess(this);
    QProcess *roslaunch = new QProcess(this);

    openTerminal->start("gnome-terminal");
    openTerminal->waitForFinished();

    cd->start("cd ~", QStringList() << "cd ~");
    cd->waitForFinished();

    cd_catkin->start("cd catkin_docking_ws/", QStringList() << "cd catkin_docking_ws/");
    cd_catkin->waitForFinished();

    roslaunch->start("roslaunch lidar_deck lidar_deck_rosbag.launch &", QStringList() << "roslaunch lidar_deck lidar_deck_rosbag.launch &");
    roslaunch->waitForFinished();
}

I have conculted many sources in order to solve the problem such as this one , I used also this , this post .为了解决这个问题,我考虑了很多资源,比如这个,我也用过这个这个帖子 But all of them seems to give confusing information and am looking for a clean and simple process.但它们似乎都提供了令人困惑的信息,并且正在寻找一个干净简单的过程。

Also I dug more into the solution and this post advises to go for detachedProcess but I am not sure it is a good idea because this post seems to follow a pretty organized structure.此外,我对解决方案进行了更多研究, 这篇文章建议 go 使用detachedProcess但我不确定这是一个好主意,因为这篇文章似乎遵循了一个非常有组织的结构。

The end result I am trying to achieve is this but, of course, passing through all the four QProcess I have above.我想要达到的最终结果是这样,但是,当然,通过我上面的所有四个QProcess

Is that possible or am I trying to handle too many processes altogether?这是可能的还是我试图完全处理太多的进程? Thank you very much for pointing to the right direction for solving this problem.非常感谢您指出解决此问题的正确方向。

Each QProcess represents a new session, so you cannot use multiple QProcess objects to execute a sequence.每个 QProcess 代表一个新的 session,因此您不能使用多个 QProcess 对象来执行一个序列。 The best way would be to create a shell script that does what you want it to, then use one QProcess instance to execute that script.最好的方法是创建一个 shell 脚本来执行您想要的操作,然后使用一个 QProcess 实例来执行该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM