Results 1 to 4 of 4

Thread: QProcess can read stderr but no stdout

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QProcess can read stderr but no stdout

    I tried to use QProcess. I had some problems with reading stdout, so I create a simple project to reproduce the problem:

    suppose to have a simple program like this:
    Qt Code:
    1. #include<stdio.h>
    2. #include <unistd.h>
    3.  
    4. int main(void)
    5. {
    6. int o,e;
    7. o = e = 0;
    8.  
    9. while(1)
    10. {
    11. printf("o %d\n", o);
    12. usleep(200000);
    13. fprintf(stderr,"e %d\n", e);
    14. usleep(200000);
    15.  
    16. o++;
    17. e++;
    18. }
    19.  
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 

    that works fine. When I run it I have the following output on the console:
    o 0
    e 0
    o 1
    e 1
    o 2
    e 2
    o 3
    e 3
    o 4
    e 4
    o 5
    ...

    then I have a simple qt project based on QProcess:

    dialog.h:
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QProcess>
    6.  
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = 0);
    17. ~Dialog();
    18.  
    19. private:
    20. Ui::Dialog *ui;
    21. QProcess extpro;
    22.  
    23. public slots:
    24. void btnpressed(void);
    25. void prtstdout(void);
    26. void prtstderr(void);
    27. };
    28.  
    29. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    and dialog.cpp:
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include <QDebug>
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10.  
    11. connect(ui->pushButton,SIGNAL(clicked()),this, SLOT(btnpressed()));
    12.  
    13. connect(&extpro, SIGNAL(readyReadStandardOutput()), this, SLOT(prtstdout()));
    14. connect(&extpro, SIGNAL(readyReadStandardError()), this, SLOT(prtstderr()));
    15. }
    16.  
    17. Dialog::~Dialog()
    18. {
    19. delete ui;
    20. extpro.kill();
    21. }
    22.  
    23.  
    24. void Dialog::btnpressed(void)
    25. {
    26. extpro.start("../zozzo-build-desktop/zozzo");
    27. }
    28.  
    29. void Dialog::prtstdout(void)
    30. {
    31. qDebug() << QString(extpro.readAllStandardOutput());
    32. }
    33.  
    34. void Dialog::prtstderr(void)
    35. {
    36. qDebug() << QString(extpro.readAllStandardError());
    37. }
    To copy to clipboard, switch view to plain text mode 

    If I try to execute obtain only:
    "e 0
    "
    "e 1
    "
    "e 2
    "
    "e 3
    "
    "e 4
    "
    "e 5
    "
    "e 6
    "
    "e 7
    "

    I can see only stderr. How can I have also stdout to have an output just like console?

    In attachment there is the projects

    thanks
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QProcess can read stderr but no stdout

    I haven't looked at all your code but are you sure that
    Qt Code:
    1. printf("o %d\n", o);
    To copy to clipboard, switch view to plain text mode 
    prints to stdout and not the current console/shell/...?

  3. #3
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess can read stderr but no stdout

    Quote Originally Posted by tbscope View Post
    I haven't looked at all your code but are you sure that
    Qt Code:
    1. printf("o %d\n", o);
    To copy to clipboard, switch view to plain text mode 
    prints to stdout and not the current console/shell/...?
    of course
    anyway try to change that line to:
    Qt Code:
    1. fprintf(stdout,"o %d\n", o);
    To copy to clipboard, switch view to plain text mode 

    thanks

  4. #4
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess can read stderr but no stdout

    Quote Originally Posted by mastupristi View Post
    of course
    anyway try to change that line to:
    Qt Code:
    1. fprintf(stdout,"o %d\n", o);
    To copy to clipboard, switch view to plain text mode 
    I have just tried
    Same behaviour

Similar Threads

  1. Read most recent output from QProcess's stdout
    By Lawand in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2010, 22:29
  2. QProcess StdErr signal question
    By devx in forum Qt Programming
    Replies: 4
    Last Post: 25th November 2009, 18:15
  3. Accessing program's own stdout/stderr
    By TemporalBeing in forum Qt Programming
    Replies: 8
    Last Post: 1st September 2009, 02:45
  4. read stdout with QProcess under Windows
    By jlbrd in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2006, 17:29
  5. Cannot get stdout from QProcess
    By johnny_sparx in forum Qt Programming
    Replies: 11
    Last Post: 3rd March 2006, 11:46

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.