Results 1 to 3 of 3

Thread: QProcess : force cout to be sent

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default QProcess : force cout to be sent

    Hello

    I've two application A and B.
    The first one - A - creates a QProcess in order to call the second one - B -.
    The first one - A - listen the second one - B -, using readyRead() signal.

    So, our B application send data to A in a loop :
    Qt Code:
    1. int i = 0;
    2. while (i<10)
    3. {
    4. std::cout << i << std::endl;
    5. i++;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The A application detects that data is ready read, and display it :
    Qt Code:
    1. QString message = "";
    2. message.append(myProcess->readAllStandardOutput());
    3. std::cout << "." << message.toStdString() << "." << std::endl;
    To copy to clipboard, switch view to plain text mode 

    And the message is :
    .0
    1
    2
    3
    4
    5
    6
    7
    8
    9.
    I need to control the message content, in order to use correctly
    So, it woul be nice if I could enforce the cout to be written immediately in the B application... Or enforce the A application to not append all the cout content.

    Example :
    Qt Code:
    1. int i = 0;
    2. while (i<10)
    3. {
    4. std::cout << i << std::endl; // Enforce this to be submitted immediately
    5. i++;
    6. }
    To copy to clipboard, switch view to plain text mode 

    And the message would be :
    .0.
    I could have 10 messages, and not just one.

    Have you got an idea ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess : force cout to be sent

    You can use "cout.flush()" to clear the output buffer, but this won't solve your problem. What you send to cout is a stream --- it doesn't contain any messages.

    Application "A" should parse the data it receives and divide it into messages for example by looking for \n. Also you shouldn't assume that you will get a whole message at once --- you might get a part of it.

    You need something like:
    Qt Code:
    1. void Something::readMessages()
    2. {
    3. buffer.append( myProcess->readAllStandardOutput() ); // note that buffer might contain some data from previous calls
    4. while( buffer.containsAtLestOneWholeMessage() ) {
    5. QString msg( buffer.takeFirstMessage() );
    6. processMessage( msg );
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    QString::endsWith() and QString::split() methods might be helpful.

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : force cout to be sent

    Thanks, this is what I will do...


Similar Threads

  1. nmake problems while building mysql driver
    By MarkoSan in forum Installation and Deployment
    Replies: 27
    Last Post: 25th May 2007, 12:57
  2. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  3. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30
  4. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 01:48
  5. QProcess / system call not working under linux. Why?
    By johnny_sparx in forum Qt Programming
    Replies: 12
    Last Post: 11th March 2006, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.