Search code examples
c++qtpipesignals-slotsqprocess

Display QProcess output in another window


I'm using the QT Creator on Ubuntu. I have GUI with a mainwindow and another window called "progress". Upon clicking a button the QProcess starts and executes an rsync command which copies a folder into a specific directory. I created a textbrowser which reads the output from the rsync command. Also clicking the button causes the "progress" window to pop up. So far so so good, now my problem. Instead of showing the rsync output in my mainwindow i want it to be in progress. I've tried several methods to get the QProcess into the progress via connect but that doesn't seem to work.

mainwindow.cpp

void MainWindow::on_pushButton_clicked()
{

         if (ui->checkBox->isChecked()
           )
       m_time ="-t";


    QObject parent;
    m_myProcess =  new QProcess();
    connect(m_myProcess, SIGNAL(readyReadStandardOutput()),this, SLOT(printOutput()));

           QString program = "/usr/bin/rsync";

           arguments << "-r" << m_time << "-v" <<"--progress" <<"-s"

               << m_dir
               << m_dir2;




           m_myProcess->start(program, arguments);

          }

progress.cpp

void Progress::printOutput()
{


  ui->textBrowser->setPlainText(m_myProcess->readAllStandardOutput());
}

I know it's pretty messy iv'e tried alot of things and haven't cleaned the code yet also I'm pretty new to c++. My goal was to send the QProcess (m_myProcess) to progress via connect but that didn't seem to work. Can you send commands like readyReadAllStandardOutput via connect to other windows (I don't know the right term )? Am I doing a mistake or is there just another way to get the output to my progress window ?


Solution

  • m_myProcess is a member of the class MainWindow and you haven't made it visible to the class Progress. That's why you have the compilation error

    m_myProcess was not declared in this scope

    What you could do:

    1. Redirect standard error of m_myProcess to standard output, such that you also print what is sent to standard error (unless you want to do something else with it). Using

      m_myProcess.setProcessChannelMode(QProcess::MergedChannels);  
      
    2. Make the process object available outside MainWindow

      QProcess* MainWindow::getProcess()
      {
         return  m_myProcess;
      }
      
    3. Read the process output line by line in Progress. It needs to be saved in a buffer because readAllStandardOutput() only return the data which has been written since the last read.

      ... // somewhere
      connect(window->getProcess(), SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput())
      ...
      
      void Progress::printOutput(){
         //bigbuffer is member
         bigbuffer.append(myProcess->readAllStandardOutput();)
         ui->textBrowser->setPlainText(bigbuffer);
      }