Pj Toopmuch
Pj Toopmuch

Reputation: 147

QProcess does not emit finished signal

I try to read out the output of a command executed in QProcess. I don't get the connection working between QProcess's finished signal and a finished handler. I tried several different ways of connecting to QProcess finished signal but no luck so far. I know that the process executes because it prints the output in the GUI's console and that the process finishes because it passes the waitForFinished method from sequential API.

Why does my code not hit onFinish or onFinishNoPams?

header:

class Test : public QObject
{
    Q_OBJECT
public:
    Test(QObject *parent = 0);

    Q_INVOKABLE void read();
public slots:
    void onFinish(int exitCode , QProcess::ExitStatus exitStatus);
    void onFinishNoPams();

private:
    QProcess *m_process;

};

cpp:

Test::Test(QObject *parent)
    : QObject(parent)
{}
void Test::read(){
    m_process=new QProcess(this);

    connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinishNoPams()));

    connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinish(int,QProcess::ExitStatus)));

    connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
        [=](int exitCode, QProcess::ExitStatus exitStatus){
    qDebug() << "not reached";
    });

    connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,&Test::onFinish);

    QString program = "lsusb";
    QStringList arguments;
    m_process->execute(program,arguments);
    m_process->waitForFinished();
    qDebug() << "reached";
    return;
}

void Test::onFinish(int exitCode , QProcess::ExitStatus exitStatus){
    qDebug() << "not reached";
}

void Test::onFinishNoPams(){
    qDebug() << "not reached";
    QByteArray out=m_process->readAllStandardOutput();
}

qml:

import test 1.0

Window {    
    Test{
        id:test
        Component.onCompleted: test.read()
    }
}

Upvotes: 0

Views: 1229

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25165

QProcess::execute() is a static method that starts the external process and waits for its completion in a blocking way. It has nothing to do with your m_process variable. What you want to use is QProcess::start().

Upvotes: 1

Related Questions