[Solved] How to use QProcess?



  • I am not so used to Qt and QML. What I want to do is start another .exe program from my program.
    So I have a .lng file which I need to send through another .exe file for converting.

    Can anyone show me how to do this?
    Seen that I could use QProcess, but reading the documentation, I really didnt feel any smarter.
    I've seen a couple of other examples as well, without really understanding how to do it, and how QProcess works.

    Thanks for any reply.


  • Moderators

    Hi,

    Going through the "QProcess":http://qt-project.org/doc/qt-5/QProcess.html doc you can see how to start a QProcess and how to pass arguments to it.
    In your case your .exe file is a program and the .lng file which you want to send to it would be the arguments to it.
    So starting QProcess would be similar to following in your case,
    @
    QString program = "D:/Sample.exe"; //Complete path to your .exe file
    QStringList arguments;
    arguments << "D:/Sample.lng"; //Complete path to your .lng file

    QProcess *myProcess = new QProcess;
    myProcess->start(program, arguments);
    @

    Please go through the QProcess doc for a complete understanding of how it works.



  • To expand on p3c0's answer... If your exe exits when it's done with the conversion you can wait for it to exit to know it's done before you proceed with whatever you needed the converted data for.

    To do that, just add a slot for the finished signal.

    @
    void myClass::someFunc()
    {
    QProcess myProcess = new QProcess(this);
    connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processDone(int, QProcess::ExitStatus)));
    myProcess->start("/path/to/exe", QStringList() << "your_file_to_process.ing");
    }

    void MyClass::processDone(int exitCode, QProcess::ExitStatus es)
    {
    if (exitCode || es != QProcess::NormalExit)
    {
    // usually a problem if it's not 0, handle it, or not
    return;
    }

    // all good, so handle your converted data here...
    }
    @

    I normally wouldn't pass this to QProcess but since it was in that function and the pointer would go out of scope without you being able to clean it up, I added that for memory clean up. Normally in that situation I would have a class member variable like mProcess so I could keep track and clean up the pointer later on. Plus it allows you to continue to access the QProcess until you're done with it.



  • Thank you!

    This works perfectly.



  • No problem! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.