Felippe Trigueiro
Felippe Trigueiro

Reputation: 109

QProcess do not run an external application

I can run an external application using the following command:

system("/home/felippe/Área\\ de\\ Trabalho/Felippe/Mestrado/C_plus_plus/Codigos/build-Registration_ITK_CMAKE-Desktop_Qt_5_12_3_GCC_64bit-Default/Registration_ITK_CMAKE")

And the application runs successfully. But the system(.) command blocks the other commands until the application finishes. So I tried to implement this command in QT using the following code:

.h

#ifndef FOO_H
#define FOO_H

#include <QObject>
#include <iostream>
#include <QProcess>

class foo : public QObject
{
    Q_OBJECT
public:
    explicit foo(QObject *parent = nullptr);

signals:


public slots:

   void process_started();

   void processError(QProcess::ProcessError error);

private:
   QProcess *process;

};

#endif // FOO_H

.cpp

#include "foo.h"

foo::foo(QObject *parent) : QObject(parent)
{
    process = new QProcess();
    bool status = QObject::connect( process, SIGNAL( started() ), this, SLOT( process_started() ) );
    connect(process, &QProcess::errorOccurred, this, &foo::processError);
    QString file = "/home/felippe/Área de Trabalho/Felippe/Mestrado/C_plus_plus/Codigos/build-Registration_ITK_CMAKE-Desktop_Qt_5_12_3_GCC_64bit-Default/Registration_ITK_CMAKE";
    process->start(file);
    std::cout << file.toStdString() << std::endl;
    std::cout << "status: " << status << std::endl;
}

void foo::process_started()
{
    std::cout << "It worked" << std::endl;
}

void foo::processError(QProcess::ProcessError error)
{
    std::cout << "error enum val = " << error << std::endl;
}

main

#include <QCoreApplication>
#include <iostream>
#include "foo.h"  

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    foo *f = new foo();


    return a.exec();
}

When I run the process->start(file); I receive a error with value of 0, but when I run process->start(file, {"sudo"}); I receive a signal that the program run successfully, but anything is showed on the screen.

I'm trying to run on UBUNTU 16.04.

So, what is happening?

Upvotes: 0

Views: 205

Answers (1)

Botje
Botje

Reputation: 31123

The most obvious difference is that system passes your string to the shell, while QProcess::start takes a command and argument list separately. I bet you will get "file not found" if you hook to the errorOccurred signal.

Solution: remove the \\ from your string, as those are only needed if a shell is involved.

Upvotes: 1

Related Questions