vendredi 25 janvier 2019

Get response from QProcess in real time

I am new in c++ programming , so i need help with the logic (code would be awesome). I want to make QTextEdit to work as a terminal like widget. I am trying to achieve that with QProcess like:

void QPConsole::command(QString cmd){

QProcess* process = new QProcess();
    connect(process, &QProcess::readyReadStandardOutput, [=](){ print(process->readAllStandardOutput()); });
    connect(process, &QProcess::readyReadStandardError, [=](){ print(process->readAllStandardError()); });
    connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){ prepareCommandLine(); return; });


    process->setProgram("/bin/bash");
    process->start();
    process->write(cmd.toStdString().c_str());
//    process->write("\n\r");

    process->closeWriteChannel();

The problem is next: If i don't close write channel (process->closeWriteChannel) than i am stuked in infinite loop. And if i do close it, than i cannot remember state (kinda makes new session) for example if i do "pwd" i get result, but if i do next "cd .." and then "pwd" the result is same as the first output of "pwd"

So, my question is is it possible to achieve some kind of real time output + having previous state remembered (like in real terminal) with QProcess or i have to do in some other way. In python i did it with subprocess calls with pipes, but i don't know what is the equilavent for c++.

An code example would be great. I have QTextEdit part (which is sending QString param to function), i need part with interaction with console.

Aucun commentaire:

Enregistrer un commentaire