2

I wonder if someone has an Answer for this Problem.
I use QProcess to start a Powershell script. The start of the QProcess is initiated from a QThread. This Thread finishes like expected but the QProcess never calls its readyReadStandardOutput() Signal nor its finished() Signal. Both are bound to 2 different slots and I can see that the script gets executed and I also know that the script finshes with an exitCode.

My goal with this is basically to read the Exitcode of the Powershell script I wrote. I tried different approaches but could not find a solution to get the Exitcode of the Powershell script which is either ran with cstdlib function system() or QProcess. So I decided I would read it from stdout. For that I would simply print the $LastExitCode before exiting the script and read that from within the C++ Program.

Thanks in advance.

Edit:

Updated example:

main.cpp

#include <QtCore/QCoreApplication>
#include <QTextStream>
#include "ManagerClass.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream wrappedStream(stdin);
    QString scriptPath;
    if (argc < 2)
    {
        std::cout << "Path to script:" << "\n";
        wrappedStream >> scriptPath;
    }
    ManagerClass manager(scriptPath);

    return a.exec();
}

ManagerClass.h

#pragma once

#include <QObject>
#include <QProcess>
#include <iostream>
#include "ExecutionThread.h"

class ManagerClass : public QObject
{
    Q_OBJECT

public:
    ManagerClass(const QString& p_scriptPath, QObject *parent = nullptr);
    ~ManagerClass();

public slots:
    void threadFinished();
    void processStarted();
    void processFinished(int exitCode, QProcess::ExitStatus exitStatus);

private:
    ExecutionThread m_thread;
    const QString SCRIPT_PATH;
};

ManagerClass.cpp

#include "ManagerClass.h"

ManagerClass::ManagerClass(const QString& p_scriptPath, QObject* parent)
    : QObject(parent), SCRIPT_PATH(p_scriptPath)
{
    m_thread.getProcessPointer();
    m_thread.registerScriptPath(p_scriptPath);
    QObject::connect(m_thread.getProcessPointer(), QOverload<int, QProcess::ExitStatus> ::of(&QProcess::finished), this, &ManagerClass::processFinished);
    QObject::connect(m_thread.getProcessPointer(), &QProcess::started, this, &ManagerClass::processStarted);
    QObject::connect(&m_thread, &ExecutionThread::finished, this, &ManagerClass::threadFinished);
    m_thread.start();
}

ManagerClass::~ManagerClass() {}

void ManagerClass::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    std::cout << " Process finished successfully." << "\n";
    std::cin.get();
}

void ManagerClass::threadFinished()
{
    std::cout << " Thread finished successfully." << "\n";
}

void ManagerClass::processStarted()
{
    std::cout << " Process started." << "\n";
}

ExecutionThread.h

#pragma once

#include <QThread>
#include <QProcess>
#include <iostream>

class ExecutionThread : public QThread
{
    Q_OBJECT

public:
    ExecutionThread();
    ~ExecutionThread();

    void run();
    void registerScriptPath(const QString& p_scriptPath);
    QProcess* getProcessPointer();

signals:
    void finished();

private:
    QProcess* m_Process;
    QString m_scriptPath;
};

ExecutionThread.cpp

#include "ExecutionThread.h"

ExecutionThread::ExecutionThread() : m_Process(new QProcess) {}

ExecutionThread::~ExecutionThread()
{
    std::cout << "ExecutionThread::~ExecutionThread()";
}

void ExecutionThread::run()
{
    QStringList commands;
    m_Process->start(m_scriptPath, commands);
    emit finished();
}

void ExecutionThread::registerScriptPath(const QString& p_scriptPath)
{
    m_scriptPath = p_scriptPath;
}
QProcess* ExecutionThread::getProcessPointer()
{
    return m_Process;
}

EDIT: I solved the issue I was facing with always getting 1 as Exitcode. It turns out that I misunderstood the Qt API for QProcess. Still I dont get Signals from that QProcess object when it finishes. The above Example uses m_Process->start() and there is no signal whenever the given powershell script finishes. As mentioned before m_Process->execute() doesnt signal anything too.

k_k
  • 83
  • 7
  • Please provide a [minimal executable example](https://stackoverflow.com/help/minimal-reproducible-example) There can be many things leading to this outcome – Ingo Mi Mar 24 '21 at 16:20
  • 1
    I added the minimalistic classes to it. – k_k Mar 24 '21 at 16:37
  • I wanted to give an Update: I tried further Debugging and I realized that the Process does not even start because the Signal started() of the QProcess Object is not emitted. Also I tried the function start() of QProcess and still there is no Response. Basically the thing I wanted to do is done, but this is quite interesting. To me this seems like a bug. – k_k Mar 25 '21 at 21:39
  • My schedule is pretty tight rn but I try to find some time to look into this weekend. Great that you figured out the first part of your question already! It might spare me some time if you update example with your discovery. – Ingo Mi Mar 25 '21 at 22:53
  • I added a compilable, linkable Source if you want to try out. – k_k Mar 26 '21 at 06:56

0 Answers0