betaros
betaros

Reputation: 602

Qt Start process in interactive shell

I've created two programs in Qt.

Program A - Console project with interactions in shell

Program B - GUI project

Now I want to start Program A from Program B. For that purpose I tried many things and ended with:

QProcess *process = new QProcess(this);

QString command = "cmd.exe";
QStringList arguments;
arguments << "/C" << settings.getPath().replace("/","\\");

process->start(command, arguments);

This starts a process, but doesn't open a cmd in Windows.

I also tried out:

QProcess *process = new QProcess(this);

QString command = "start";
QStringList arguments;
arguments << "cmd.exe" << "/C" << settings.getPath().replace("/","\\");

process->start(command, arguments);

It looks like the process is started in the background. In that case I am unable to use my command line program.

How can I start an interactive cmd?

Upvotes: 1

Views: 2382

Answers (1)

MauriceRandomNumber
MauriceRandomNumber

Reputation: 412

Which devenv are you using for each of the projects?

Depending on the dev-env you are using, you could try setting up your console project to run in a cmd.exe which is not in the background (you would need to look at the manual from your dev-env in this case)

Other thing: Can you start your (compiled) console project via

system("projecta.exe"); ?

from project b?

If you are using Visual Studio compiler, take a look at this : QProcess with 'cmd' command does not result in command-line window

Which uses following code (note the CREATE_NEW_CONSOLE flag):

#include <QProcess>
#include <QString>
#include <QStringList>
#include "Windows.h"

class QDetachableProcess 
        : public QProcess {
public:
    QDetachableProcess(QObject *parent = 0) 
        : QProcess(parent) {
    }
    void detach() {
       waitForStarted();
       setProcessState(QProcess::NotRunning);
    }
};

int main(int argc, char *argv[]) {
    QDetachableProcess process;
    QString program = "cmd.exe";
    QStringList arguments = QStringList() << "/K" << "python.exe";
    process.setCreateProcessArgumentsModifier(
                [](QProcess::CreateProcessArguments *args) {
        args->flags |= CREATE_NEW_CONSOLE;
        args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
    });
    process.start(program, arguments);
    process.detach();
    return 0;
}

Upvotes: 1

Related Questions