Results 1 to 11 of 11

Thread: How to display the output of a process in a TextEdit widget in real time?

  1. #1
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to display the output of a process in a TextEdit widget in real time?

    Hi,

    I have an application that when run from shell, gives many messages, variable and their data as output in the same shell.

    Now, i am running that application with the help of QProcess and i want the resulting ouput to display in my Text Edit widget of my GUI in real time....i.e. as the appication is producing output, it should be appearing in TextEdit concurrently.

    I browse the google for the problem and found that people are using readLineStdout() which seems like not a slot provided by QProcess but they are using it as if it is.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    all what you need it's to use QProcess::readyReadStandardOutput/QProcess::readyReadStandardError signals and create slots for these signals, then in these slots you should call QProcess::readAllStandardOutput/QProcess::readAllStandardError and add results in a text edit.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    hmmm..right. I used following code but doesn't display anything in my TextEdit.
    Qt Code:
    1. ...
    2. QObject::connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError(myProcess)));
    3. QObject::connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText(myProcess)));
    4. myProcess->start(program);
    To copy to clipboard, switch view to plain text mode 

    Definition of updateError and updateText:
    Qt Code:
    1. void MainWindow::updateError(QProcess *myProcess)
    2. {
    3. QByteArray data = myProcess->readAllStandardError();
    4. textEdit_verboseOutput->append(QString(data));
    5. }
    6.  
    7. void MainWindow::updateText(QProcess *myProcess)
    8. {
    9. QByteArray data = myProcess->readAllStandardOutput();
    10. textEdit_verboseOutput->append(QString(data));
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    sigh, you code won't work.
    try this
    Qt Code:
    1. connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
    2. connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
    3. myProcess->start(program);
    4. ...
    5. void MainWindow::updateError()
    6. {
    7. QByteArray data = myProcess->readAllStandardError();
    8. textEdit_verboseOutput->append(QString(data));
    9. }
    10.  
    11. void MainWindow::updateText()
    12. {
    13. QByteArray data = myProcess->readAllStandardOutput();
    14. textEdit_verboseOutput->append(QString(data));
    15. }
    To copy to clipboard, switch view to plain text mode 
    BUT, myProcess must be a class member of MainWindow or you can cast QObject::sender to QProcess.
    PS, you should read more about Signal&Slot mechanism.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Angry Re: How to display the output of a process in a TextEdit widget in real time?

    Still not working. The process is however running fine but TextEdit widget remains empty.
    My MainWindow class
    class MainWindow : public QMainWindow, private Ui::MainWindow
    {
    Q_OBJECT
    public:
    QProcess *myProcess;
    MainWindow(QMainWindow *parent = 0);

    private slots:
    void updateError();
    void updateText();
    };
    MainWindow.cpp
    Qt Code:
    1. void MainWindow::On_pushButton_startAnalysis_Click()
    2. {
    3. QObject *parent;
    4. myProcess= new QProcess(parent);
    5.  
    6. QString program = "./memviz ";
    7. QCoreApplication::processEvents();
    8. connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
    9. connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
    10. myProcess->start(program);
    11. }
    12.  
    13. void MainWindow::updateError()
    14. {
    15. QByteArray data = myProcess->readAllStandardError();
    16. textEdit_verboseOutput->append(QString(data));
    17. }
    18.  
    19. void MainWindow::updateText()
    20. {
    21. QByteArray data = myProcess->readAllStandardOutput();
    22. textEdit_verboseOutput->append(QString(data));
    23. }
    To copy to clipboard, switch view to plain text mode 

    Is some error still hiding there from my egle eyes?

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    what do you see in console when this code works?
    Qt Code:
    1. void MainWindow::On_pushButton_startAnalysis_Click()
    2. {
    3. QObject *parent;
    4. myProcess= new QProcess(parent);
    5.  
    6. QString program = "./memviz ";
    7. QCoreApplication::processEvents();
    8. qDebug() << connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
    9. qDebug() << connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
    10. myProcess->start(program);
    11. }
    12.  
    13. void MainWindow::updateError()
    14. {
    15. QByteArray data = myProcess->readAllStandardError();
    16. qDebug() << data;
    17. textEdit_verboseOutput->append(QString(data));
    18. }
    19.  
    20. void MainWindow::updateText()
    21. {
    22. QByteArray data = myProcess->readAllStandardOutput();
    23. qDebug() << data;
    24. textEdit_verboseOutput->append(QString(data));
    25. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    Oh its giving following error

    Qt Code:
    1. Object::connect: No such signal QProcess::readyReadStdError()
    2. Object::connect: (receiver name: 'MainWindow')
    3. Object::connect: No such signal QProcess::readyReadStdOutput()
    4. Object::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

    But this error should also have appeared in the texEdit_verboseOutput.

    I said the program is working fine because i was actually generating an xml file with my 'memviz' application which is generated finely. Sorry din check the console earlier.

  8. #8
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    Any idea about this error? Isn't it weird QProcess::readyReadStdError() is a signal provided by QProcess as i see it in Qt Assistant but still i am getting 'no such signal error'.

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    take a look at docs: there are only these signals QProcess::readyReadStandardError && QProcess::readyReadStandardOutput.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    658
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    set the
    void QProcess::setProcessChannelMode ( ProcessChannelMode mode )

    to QProcess::MergedChannels
    "Behind every great fortune lies a crime" - Balzac

  11. #11
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display the output of a process in a TextEdit widget in real time?

    Ah...stupid me.

    Thanks. It's working now.

Similar Threads

  1. real time data display
    By hammer256 in forum Qt Programming
    Replies: 13
    Last Post: 25th March 2013, 17:47
  2. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 06:34
  3. Display the camera frame in real time
    By alex_lue in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2007, 11:31

Tags for this Thread

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.