Results 1 to 8 of 8

Thread: Run executable from within C++/Qt

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question Run executable from within C++/Qt

    I'd like to run another program (a windows exe file) from within my C++/Qt code.

    I was wondering if Qt offers anything to that effect and how to catch the results (a line of text)
    so that my own code could continue using it?

    I want to run "ent.exe -file myfile -f"

    Thanks!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Run executable from within C++/Qt

    You can use QProcess and capture the stdout channel of the process to get the results
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    timmu (3rd January 2013)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Run executable from within C++/Qt

    Dear Santosh,

    Thank you for your help.
    The following code compiled and executed:

    Qt Code:
    1. QObject *parent;
    2. QString program = "./ent";
    3. QStringList arguments;
    4. arguments << "-b" << "-t" << "input.txt";
    5.  
    6. QProcess *myProcess = new QProcess(parent);
    7. myProcess->start(program, arguments);
    To copy to clipboard, switch view to plain text mode 

    1. However I now need to catch the results into a QString. What is the best way to do that?

    2. Also, can I be certain that the rest of the program waits before this process is executed and
    what happens if some error happens. How to catch an error?

    Thanks!

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Run executable from within C++/Qt

    1. However I now need to catch the results into a QString. What is the best way to do that?

    2. Also, can I be certain that the rest of the program waits before this process is executed and
    what happens if some error happens. How to catch an error?
    Qt Code:
    1. QString program = "./ent";
    2. QStringList arguments;
    3. arguments << "-b" << "-t" << "input.txt";
    4.  
    5. QProcess *myProcess = new QProcess(parent);
    6. myProcess->start(program, arguments);
    7.  
    8. if(myProcess->waitForStarted(5000))
    9. {
    10. QByteArray out_data = myProcess->readAllStandardOutput();
    11. QString out_string(out_data);
    12. qDebug() << out_string.toStdString().c_str();
    13. }
    14. else
    15. {
    16. qDebug() << "myProcess did not start in time";
    17. }
    To copy to clipboard, switch view to plain text mode 

    This is just one simple way. You could use signal/slots also, please refer Qt Documentation for QProcess, it describes much of what you want
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    timmu (3rd January 2013)

  7. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Run executable from within C++/Qt

    Dear Santosh,

    Thanks again.
    The below code makes a QString that is empty as this does not print anything on the screen

    Qt Code:
    1. QObject *parent;
    2. QString program = "./ent";
    3. QStringList arguments;
    4. arguments << "-b" << "-t" << "input.txt";
    5.  
    6. QProcess *myProcess = new QProcess(parent);
    7. myProcess->start(program, arguments);
    8.  
    9.  
    10. if(myProcess->waitForStarted(5000))
    11. {
    12. QByteArray out_data = myProcess->readAllStandardOutput();
    13. QString out_string(out_data);
    14. qDebug() << out_string.toStdString().c_str();
    15. cout << "results: " << out_string.toLocal8Bit().constData() << "\n";
    16. }
    17. else
    18. {
    19. qDebug() << "myProcess did not start in time";
    20. }
    To copy to clipboard, switch view to plain text mode 

    I know that the other program works because this gives me the correct answer:
    Qt Code:
    1. system("./ent -b -t input.txt");
    To copy to clipboard, switch view to plain text mode 
    Do you advise against using the latter version?
    Last edited by timmu; 3rd January 2013 at 10:13.

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Run executable from within C++/Qt

    Try this
    Qt Code:
    1. myProcess->setReadChannel(QProcess::StandardOutput);
    2. myProcess->waitForReadyRead();
    3. myProcess->readAllStandardOutput();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    timmu (3rd January 2013)

  10. #7
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Run executable from within C++/Qt

    Dear Santosh,

    Thanks a lot. This beautifully solved it and everything works well now.


    Qt Code:
    1. QObject *parent;
    2. QString program = "./ent";
    3. QStringList arguments;
    4. arguments << "-b" << "-t" << "input.txt";
    5.  
    6. QProcess *myProcess = new QProcess(parent);
    7. myProcess->start(program, arguments);
    8.  
    9. if(myProcess->waitForStarted(5000))
    10.  
    11. {
    12.  
    13. myProcess->setReadChannel(QProcess::StandardOutput);
    14. myProcess->waitForReadyRead();
    15.  
    16. QByteArray out_data = myProcess->readAllStandardOutput();
    17. QString out_string(out_data);
    18. qDebug() << out_string.toStdString().c_str();
    19. cout << "results: " << out_string.toLocal8Bit().constData() << "\n";
    20. }
    21.  
    22. else
    23. {
    24. qDebug() << "myProcess did not start in time";
    25. }
    To copy to clipboard, switch view to plain text mode 

    My last question is about this:

    Qt Code:
    1. if(myProcess->waitForStarted(5000))
    To copy to clipboard, switch view to plain text mode 

    I'm assuming it means that the system waits for 5000 ms. Does it wait starting from when the external process was started or when it finshed?

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Run executable from within C++/Qt

    It waits from the time the waitForStarted() call is made to time when the external process started. If the process is already started waitForStarted() will return right away.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. The following user says thank you to Santosh Reddy for this useful post:

    timmu (3rd January 2013)

Similar Threads

  1. Could not find executable
    By jeff28 in forum Qt Quick
    Replies: 6
    Last Post: 31st August 2012, 14:49
  2. run a executable file
    By Ali Reza in forum Newbie
    Replies: 4
    Last Post: 17th June 2012, 20:09
  3. QT + SQL + executable
    By anouar2002 in forum Newbie
    Replies: 1
    Last Post: 22nd January 2012, 22:56
  4. Help in the executable
    By mukunda in forum Qt Programming
    Replies: 6
    Last Post: 5th July 2011, 07:59
  5. Static Executable
    By perseo in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2008, 21:39

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.