Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. New to Qt, could use some help with Qprocess
Qt 6.11 is out! See what's new in the release blog

New to Qt, could use some help with Qprocess

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    B-80
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        B-80
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved