Results 1 to 4 of 4

Thread: [QProcess] Calling console program

  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [QProcess] Calling console program

    Hello all,

    First let me say that I've tried to look through these forums for an answer to my problem, but as of yet, have been unsuccessful, so here it goes.

    I have a simple utility program that is command line based. I have been developing a GUI to get the required parameters and then pass them to the command line program when my "Process" button is clicked. Here is the code that is executed when the button is clicked:

    Qt Code:
    1. QString program = "/home/bgarrison/code/myUtils";
    2. QStringList arguments;
    3.  
    4. QProcess *myProcess = new QProcess(this);
    5. myProcess->start(program, arguments);
    6.  
    7. myProcess->waitForFinished();
    8. QString strOut = myProcess->readAllStandardOutput();
    9.  
    10. qDebug() << strOut;
    To copy to clipboard, switch view to plain text mode 

    If I run 'myUtils' with no arguments, I should get "This is a test." on std::out. However, my debug print prints ""...

    I'm at a loss for why this is so.

    I've tried the examples in the documentation:

    Qt Code:
    1. QString program = "./path/to/Qt/examples/widgets/analogclock";
    2. QStringList arguments;
    3. arguments << "-style" << "motif";
    4.  
    5. QProcess *myProcess = new QProcess(this);
    6. myProcess->start(program, arguments);
    To copy to clipboard, switch view to plain text mode 

    And it brings up the AnalogClock example just fine. I've even tried to call several of the other examples, which also worked.

    Why can I not call my command-line based program?

    Thanks for any and all advice.

  2. #2
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QProcess] Calling console program

    try to use the signal readyReadStandardOutput () and in slot get the data.

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

    Default Re: [QProcess] Calling console program

    navi(avatar) is right ... u can get the readAllStandardOutput() or readAllStandardError() only after the QProcess emits readyRead() signal .. be sure u are receiving std output or std error ... so wait till u receives the signal readyReadStandardOutput() or readyReadStandardError() ..
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QProcess] Calling console program

    Ok, I still am doing something wrong, but I took the advice both of you presented me. When I click my button, which invokes my command-line based program, it does the following:

    Qt Code:
    1. QString program = "/home/bgarrison/code/myUtils";
    2. QStringList arguments;
    3.  
    4. myProcess = new QProcess(this);
    5.  
    6. connect (myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));
    7. connect (myProcess, SIGNAL(readyReadStandardError()), this, SLOT(printError()));
    8.  
    9. myProcess->start(program, arguments);
    10.  
    11. myProcess->waitForFinished();
    To copy to clipboard, switch view to plain text mode 

    printOutput() is a private SLOT in my class and it does the following:

    Qt Code:
    1. void cMyUtils::printOutput()
    2. {
    3. ui->e_Log->append("Got to printOutput()"); // TextEdit to see results
    4.  
    5. QByteArray byteArray = myProcess->readAllStandardOutput();
    6. QStringList strLines = QString(byteArray).split("\n");
    7.  
    8. foreach (QString line, strLines){
    9. ui->e_Log->append(line);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Dialog_FileSplitter::printError()
    2. {
    3. ui->e_Log->append("Got to printError()");
    4.  
    5. QByteArray byteArray = myProcess->readAllStandardError();
    6. QStringList strLines = QString(byteArray).split("\n");
    7.  
    8. foreach (QString line, strLines){
    9. ui->e_Log->append(line);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    This resulted in me being able to see why the program was not starting :)

    Got to printError()
    /home/bgarrison/code/myUtils: error while loading shared libraries: libboost_regex.so.1.41.0: cannot open shared object file: No such file or directory
    Looks like I have a problem linking the Boost libraries, not calling the program using QProcess. Thanks for all of your help!

Similar Threads

  1. how to print a matrix to a console in GUI program
    By elflord in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2009, 12:16
  2. QProcess-startDetached and Console screen problem
    By ramazangirgin in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 08:05
  3. QProcess start a console program
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 12:38
  4. QProcess and console programs
    By bond_e in forum Qt Programming
    Replies: 10
    Last Post: 13th July 2007, 08:39
  5. Console Program Problem
    By ball in forum Qt Programming
    Replies: 9
    Last Post: 28th May 2006, 09:05

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.