Results 1 to 5 of 5

Thread: Qt5/C++ - QProcess, capture terminal data

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt5/C++ - QProcess, capture terminal data

    Qt5.5.1
    QtCreator 3.5.1
    Windows 7

    Hello,

    I'm runnning a QProcess, and it runs fine. (7z, command line version of 7Zip)
    The command I'm running outputs to the command prompt console continuously changing the % value
    of the un-zipping progress. Run from the command prompt, shows:-
    Qt Code:
    1. 15% - myFile.zip
    To copy to clipboard, switch view to plain text mode 
    What I can't work out is how to redirect the console to my program, so I can show a progress bar.
    I underatand the QPogressBar procedures.
    Thanks for any advice offered.

    Regards
    Qt Code:
    1. process->setProcessChannelMode(QProcess::ForwardedChannels);
    2. process->start(cmd);
    3. do {
    4. delay(1000); //processes events
    5. qDebug() << "o - " << (process->readAllStandardOutput());
    6. qDebug() << "e - " << (process->readAllStandardError());
    7. } while(busy); //flag, changes when QProcess finishes, signal connected to slot
    8. process->waitForFinished(-1); // -1, forever
    9. process->close();
    To copy to clipboard, switch view to plain text mode 
    Output:-
    Qt Code:
    1. o - ""
    2. e - ""
    3. o - ""
    4. e - ""
    5. ...
    6. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - QProcess, capture terminal data

    Sometimes shell programs don't actually write to the output streams but to the terminal directly.

    In such cases they often have command line options to specifically enable writing to the streams.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - QProcess, capture terminal data

    Hello anda_skoa,

    Thanks for your reply.
    I've added some options to the called program and reduced my QProcess to this:-
    Qt Code:
    1. process->setProcessChannelMode(QProcess::ForwardedChannels);
    2. process->start(cmd);
    3. do {
    4. delay(500); //process events
    5. } while(busy);
    6. process->waitForFinished(-1);
    7. process->close();
    To copy to clipboard, switch view to plain text mode 
    Application output tab, when the program is run inside QtCreator:-
    Qt Code:
    1. 0%
    2.  
    3.  
    4. 0% - myData.img
    5.  
    6.  
    7. 1% - myData.img
    8.  
    9.  
    10. 2% - myData.img
    11. ...
    12. ...
    To copy to clipboard, switch view to plain text mode 
    Of course, If I take out the first program line there is no output.
    Is there any way I can capture the output from 'process->setProcessChannelMode(QProcess::ForwardedChannels )'.

    Regards
    Last edited by jimbo; 1st January 2016 at 11:54.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - QProcess, capture terminal data

    If you don't forward the channels but connect slots to the two readyRead signals, do these slots get invoked?

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    jimbo (1st January 2016)

  6. #5
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - QProcess, capture terminal data

    Hello anda_skoa.

    Thanks for your reply and help.
    'readyReadStandardOutput'. No help.
    'readyReadStandardError'. Does help.
    I can get the '%' from this.

    Regards
    Qt Code:
    1. process = new QProcess(this);
    2. QObject::connect(process, SIGNAL(started()), this, SLOT(processStarted()));
    3. QObject::connect(process, SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput()));
    4. QObject::connect(process, SIGNAL(readyReadStandardError()),this,SLOT(readyReadStandardError()));
    5. QObject::connect(process, SIGNAL(finished(int)), this, SLOT(processFinished()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::readyReadStandardError() //slot
    2. {
    3. QString errorString;
    4. errorString.append(process->readAllStandardError());
    5. qDebug() << "readyReadStandardError - " << errorString;
    6. }
    To copy to clipboard, switch view to plain text mode 

    SLOT 'readyReadStandardError' gives:-
    Qt Code:
    1. readyReadStandardError - " 0%"
    2. readyReadStandardError - "\r \r 0% - myData.img"
    3. readyReadStandardError - "\r \r 1% - myData.img"
    4. readyReadStandardError - "\r \r 2% - myData.img"
    5. readyReadStandardError - "\r \r 3% - myData.img"
    6. ...
    7. ...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QProcess::pid() gives invalid data
    By shaolin in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2011, 13:31
  2. Replies: 2
    Last Post: 3rd February 2011, 15:48
  3. Opening A Terminal using QProcess
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 14:51
  4. Replies: 3
    Last Post: 5th July 2009, 17:22
  5. recieving QProcess::write data
    By node_ex in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2008, 12:18

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.