Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProcess, What does it do?



  • I've been reading about QProcess and I can't understand why it should be used!
    In the documentation

      QString program = "./path/to/Qt/examples/widgets/analogclock";
      QStringList arguments;
      arguments << "-style" << "fusion";
    
      QProcess *myProcess = new QProcess(parent);
      myProcess->start(program, arguments);  // is this supposed to run the example analogclock ?
    

  • Lifetime Qt Champion

    Hi,

    QProcess is used to start an executable.

    So if ./path/to/Qt/examples/widgets/analogclock is indeed an executable, it should get started and show you something. If that's not the case, then you should check whether it started properly, ended properly, wrote something on its standard output or error channel.



  • @SGaist
    Thanks very much

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QProcess P;
        qDebug()<<"Start the process";
        QString executableName="D:\SmallTryApp"; //Here is the executable which was created using deployment tool
        P.start(executableName);
        qDebug()<<"Let's go!";
         return a.exec();
    }
    

    but it gives me the following output
    Start the process
    Let's go!

    the executable ,i'm expecting to run, is just a QWidget with some funny colour with a label.
    Am I doing something wrong here ?


  • Lifetime Qt Champion

    Several in fact:

    • \ is an escape character when dealing with strings (nothing Qt specific here), so either use \\ or since you are using Qt, using the unix notation for all platform and be done with it.
    • You still don't check that everything is started correctly or if there's something going wrong.

    One last thing to check, can you start SmallTryApp from the Windows explorer ?



  • @SGaist
    Thanks very much , You're right
    I checked it in Linux and it works fine.

    However , in windows it remains as it was. yes SmallTryApp can be started from windows explorer.
    What exactly you mean when you said check that everything is started correctly !!

    one more thing if I might add... Linux and QT are awesome together.



  • @hjohn

    However , in windows it remains as it was.

    Show your new Windows code? If you made it work under Linux, there you must have changed the executableName = ... line. When you went back to Windows, did you alter it there too? If not, and it still reads QString executableName="D:\SmallTryApp";, you have not acted on @SGaist's admonition about backslash characters. You will need one of:

    QString executableName="D:\\SmallTryApp"; 
    QString executableName="D:/SmallTryApp"; 
    

    Assuming you actually have a D:\SmallTryApp.exe file which works correctly, one of those will run it.

    What exactly you mean when you said check that everything is started correctly !!

    Most obviously, QProcess::start() does not return a value, but you cannot assume it will have successfully started your sub-process (as you have discovered, for example when the path is wrong). You need to look through the doc page at things like the started signal or the waitForStarted() function. Plus the errorOccurred signal, which you are likely getting. If you do these you would end up with a reason/message if the process could not be successfully run.



  • @JonB
    Thank you.

    The instruction given by @SGaist were strictly followed and the necessary changes were made.
    Linux code is working and so is windows', I can't figure out the mistake but both codes are working good.

    Previously in windows

    qDebug()<<P.state();
    

    and the output was ' QProcess::ProcessState(notRunning)'.
    But now its running.
    So thank you.



  • @hjohn
    So it sounds like you are saying you have indeed done whatever (one of the two changes I wrote) so that it now works under Windows as well as Linux, right?

    qDebug()<<P.state();
    and the output was ' QProcess::ProcessState(notRunning)'.

    You have put that line immediately after your P.start(). What do you expect? :) QProcess::start() in effect "begins the process of starting the sub-process", and then returns immediately to your calling code. At this point, depending on timing/OS/which way the wind is blowing, your sub-process could be not yet started/started and running/completed and exited --- any of those! So give it a chance --- you'd probably have to let some time elapse before checking the status to see it return running.


Log in to reply