Emanuele
Emanuele

Reputation: 2384

How to execute multiple QProcess in Qt5

I have been struggling on how to execute multiple processes using QProcess. I was able to execute a single process, e.i. opening a terminal 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)

2) navigate to cd ~

3) Go to preferred directory cd catkin_docking_ws/

4) Launch proper application 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.

The end result I am trying to achieve is this but, of course, passing through all the four QProcess I have above.

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.

Upvotes: 0

Views: 1481

Answers (1)

e8johan
e8johan

Reputation: 2939

Each QProcess represents a new session, so you cannot use multiple QProcess objects to execute a sequence. 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.

Upvotes: 3

Related Questions