Important: Please read the Qt Code of Conduct -

[SOLVED] Alternative to system()

  • Hi !

    I am looking for an alternative to system(), in order to execute a command line that looks like this :
    @ system("myexecutable -ip <ip address> -pw <password> -othercommand > text.txt"); @

    I tried to use QProcess for this, but I can't make it work.
    Does someone have an idea of something I could use instead, or maybe the correct syntax when using QProcess::execute ?

    Thank you !

  • Moderators

    On Windows, you need to use QProcess to launch cmd.exe, and then provide your command line through the /C parameter, like this:
    QProcess::startDetached("cmd.exe /C myexecutable -ip <ip address> -pw <password> -othercommand > text.txt");

    You need to experiment with using QStringList to pass arguments, it usually takes several tries to make it work.

    On Linux and Mac, the line you posted is correct and should work.

  • Moderators

    Oh sorry, I have missed the redirection. You need to use "QProcess::setStandardOutputFile": (or process).

  • Thank you !
    I will try this out.

    Edit : I had some trouble with setStandardOutputFile, so I did @QProcess::execute("cmd.exe /C myexecutable -ip <ip> -pw <pw> -othercommand > text.txt");@ and it works !
    I was missing the "cmd.exe /C" part.

Log in to reply