Results 1 to 3 of 3

Thread: Lost line with QProcess

  1. #1
    Join Date
    May 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Lost line with QProcess

    I am having problems with the following short program I wrote with Qt 4.1.2

    Qt Code:
    1. #include <QString>
    2. #include <iostream>
    3. #include <QProcess>
    4.  
    5. using namespace std;
    6.  
    7. void ProcessLine( QString );
    8.  
    9. int main()
    10. {
    11. QProcess *gzip = new QProcess;
    12. QString line;
    13. gzip->start("gunzip in.txt.gz -c");
    14.  
    15. while (gzip->waitForReadyRead()) {
    16. while (gzip->canReadLine()) {
    17. line = gzip->readLine();
    18. ProcessLine( line );
    19. }
    20. }
    21. }
    22.  
    23. void ProcessLine( QString line ) {
    24. cout << line.toStdString();
    25. }
    To copy to clipboard, switch view to plain text mode 

    my test file in.txt.gz contains the following all zipped
    this is a test 1
    this is a test 2
    this is a test 3
    this is a test 4
    this is a test 5
    this is a test 6
    this is a test 7
    this is a test 8

    when I compile and run my code I get the following output.
    this is a test 2
    this is a test 3
    this is a test 4
    this is a test 5
    this is a test 6
    this is a test 7
    this is a test 8

    Notice that I have somehow lost the first line of the file.
    every once in a while I do end up getting the first line.

    Can someone Please tell me what's happing to the first line?
    What is wrong with my code?
    Last edited by wysota; 18th May 2006 at 10:37.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Lost line with QProcess

    Try put \n before first line
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Lost line with QProcess

    Try doing it the proper way -- connect the readyReadStandardOutput() signal from your process to a slot and read the data there, and don't read a single line, but rather all of them at once.

    Qt Code:
    1. QProcess *process = new QProcess(...);
    2. connect(process, SIGNAL(readyReadStandardOutput ()), this, SLOT(fetchOutput()));
    3. connect(process, SIGNAL(finished ( int, QProcess::ExitStatus )), this, SLOT(fetchRest()));
    4. process->start("...");
    5. //...
    6.  
    7. void thisClass::fetchOutput(){
    8. QProcess *proc = (QProcess*)sender();
    9. while(proc->canReadLine()){
    10. cout << proc->readLine().constData() << endl;
    11. }
    12. }
    13.  
    14. void thisClass::fetchRest(){
    15. QProcess *proc = (QProcess*)sender();
    16. cout << proc->readAllStandardOutput().constData();
    17. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.