New to Qt, could use some help with Qprocess



  • I am trying to read and write from a qprocess right now. I made a little test program that takes input and redisplays it on the screen in a loop. Here is my code from Qt

    @QString path = "./test";

        tcd = new QProcess(this);
        QStringList args;
        args << "";
        tcd->start(path,args);
    
        if(!tcd->waitForStarted(3000))
        {
            stdoutput->append("<h1><font color=red>There was a problem starting the software, please try running the program again.</font></h1>");
    
        }
        tcd->write("hellon");
        tcd->write("hellon");
        tcd->write("hellon");
        tcd->write("hellon");
        //tcd->write("quitn");
    

    QObject::connect(tcd, SIGNAL(readyReadStandardOutput()), this, SLOT(appendTextBox()));
    This won't work unless I send that last quit command (which terminates my test program).
    @
    Here's my read command:

    @void TCD2_GUI::appendTextBox(){
    stdoutput->append("new output available: n");
    QByteArray newData = tcd->readAllStandardOutput();
    stdoutput->append(QString::fromLocal8Bit(newData));
    }@
    if I send quit, I will get all the output from the program at once, including everything I have sent it.

    What am I doing wrong here?

    here is the code from the program:

    @int main(int argC[], char* argV[]){
    printf("Welcome!n");
    char* input = malloc(160);
    gets(input);

    while(strcmp(input,"quit") != 0)
    {
        printf("Got input %sn", input);
        gets(input);
    
    }
    

    }@

    I also noticed that if I call close on the Qprocess, I wont read anything, it's only when I send the program the "quit" message



  • Normally, stdout is buffered. So you should

    @
    fflush(stdout);
    @

    in your test program after printf'ing the input.

    You might also have a look at Qt's "QTextStream":http://doc.qt.nokia.com/4.7/qtextstream.html and "QDataStream":http://doc.qt.nokia.com/4.7/qdatastream.html classes for the io.



  • Ahh, Thanks a lot.

    I thought c flushed output on a newline, I guess I am mistaken.

    I appreciate the help a ton! This has been bothering me for a while.



  • stdout is line buffered if attached to a terminal, block buffered else.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.