I'm trying to write an editor for the ruby programming language using Qt 4. This editor should be able to run a ruby script written by the user and display the output and error messages from the script in a QListView, using a different color for the latter. To do this, I use a QProcess. I connect the readyReadStandardError and readyReadStandardOutput signals from the process with two slots which set the read channel respectively to StandardError and StandardOutput, read the content of the channel and display them.

Having written this code, I tried running a ruby script which, for 10 times, writes a string to standard error and one to standard output. The result should have been this:

warning 0
message 0
warning 1
message 1
...
warning 9
message 9


where the warnings are the text written to standard error and the messages are the text written to standard output. Instead, what I got is this:


warning 0
warning 1
...
warning 9
message 0
message 1
...
message 9


Besides, the slots connected to the readyStandardError and readyStandardOutput are called only once, while I expected each of them to be called ten times (one for each message). Does anyone know what I'm doing wrong or how to achieve the result I want?

Here's a simplified version of the application. It consists of a simple QPushButton which launches the ruby script and writes the output to the terminal.

This is the header file for the MyApp class, which contains the QProcess:
Qt Code:
  1. #ifndef MYAPP_H
  2. #define MYAPP_H
  3.  
  4. #include <QApplication>
  5. #include <QProcess>
  6.  
  7. class MyApp: public QApplication{
  8.  
  9. Q_OBJECT
  10.  
  11. public:
  12.  
  13. MyApp(int argc, char ** argv);
  14. ~MyApp();
  15.  
  16. public slots:
  17. void displayOutputMsg();
  18. void displayErrorMsg();
  19. void runRuby();
  20.  
  21. private:
  22. QProcess * m_proc;
  23. };
  24.  
  25. #endif
To copy to clipboard, switch view to plain text mode 
and here's the implementation:
Qt Code:
  1. #include "app.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <QStringList>
  5.  
  6. MyApp::MyApp(int argc, char ** argv): QApplication(argc, argv), m_proc(new QProcess(this)){
  7. connect(m_proc, SIGNAL(readyReadStandardOutput()),this, SLOT(displayOutputMsg()));
  8. connect(m_proc, SIGNAL(readyReadStandardError()),this, SLOT(displayErrorMsg()));
  9. }
  10.  
  11. MyApp::~MyApp(){}
  12.  
  13. void MyApp::displayOutputMsg(){
  14. m_proc->setReadChannel(QProcess::StandardOutput);
  15. QByteArray msg = m_proc->read(1000);
  16. std::cout << "Output: "<< (msg.data())<<"\n";
  17. }
  18.  
  19. void MyApp::displayErrorMsg(){
  20. m_proc->setReadChannel(QProcess::StandardError);
  21. QByteArray msg = m_proc->read(1000);
  22. std::cout << "Error: "<< (msg.data())<<"\n";
  23. }
  24.  
  25. void MyApp::runRuby(){
  26. args << "-w" << "/home/stefano/documenti/scripts/prova.rb";
  27. m_proc->start("ruby", args);
  28. }
To copy to clipboard, switch view to plain text mode 

This is the main.cpp file:
Qt Code:
  1. #include "app.h"
  2. #include <QPushButton>
  3. #include <QWidget>
  4. #include <QLayout>
  5.  
  6. int main(int argc, char ** argv){
  7. MyApp * app = new MyApp(argc, argv);
  8. QWidget * w = new QWidget;
  9. QLayout * l = new QHBoxLayout(w);
  10. QPushButton * ruby = new QPushButton("Run &ruby", w);
  11. l->addWidget(ruby);
  12. // QPushButton * python = new QPushButton("Run &python", w);
  13. // l->addWidget(python);
  14. app->connect( ruby, SIGNAL(clicked()), SLOT(runRuby()));
  15. // app->connect( python, SIGNAL(clicked()), SLOT(runPython()));
  16. w->show();
  17. app->exec();
  18. }
To copy to clipboard, switch view to plain text mode 

Thanks in advance