Passing multiple parameters to QProcess
-
I am having an issue with passing multiple parameters to QProcess -> QStringList.
Is there a way to verify the actual contents of
QStringList BEFORE running QProcess start?PS QProcess works, verified with connect error and status , with single params, but no leading or terminating spaces.
QStringList params;
params << "-maximized"; // works fine by itself
params << "-e" << "lsusb"; // QProcess starts but terminates
//
QP->start("xterm", params);</pre></pre> -
You can redirect standard output & error that way:
QProcess ftp; QObject::connect (&ftp, &QProcess::readyReadStandardOutput, [&](){ QByteArray data = ftp.readAllStandardOutput(); auto coutList = data.split('\n'); qDebug() << coutList; }); QObject::connect (&ftp, &QProcess::readyReadStandardError, [&](){ QByteArray data = ftp.readAllStandardError(); auto coutList = data.split('\n'); qDebug() << coutList; }); ftp.start("wget", QStringList() << "-P" << url.toString()); return ftp.waitForFinished(); -
I am having an issue with passing multiple parameters to QProcess -> QStringList.
Is there a way to verify the actual contents of
QStringList BEFORE running QProcess start?PS QProcess works, verified with connect error and status , with single params, but no leading or terminating spaces.
QStringList params;
params << "-maximized"; // works fine by itself
params << "-e" << "lsusb"; // QProcess starts but terminates
//
QP->start("xterm", params);</pre></pre>@AnneRanch said in Passing multiple parameters to QProcess:
Is there a way to verify the actual contents of
QStringList BEFORE running QProcess start?qDebug() << params; -
You can redirect standard output & error that way:
QProcess ftp; QObject::connect (&ftp, &QProcess::readyReadStandardOutput, [&](){ QByteArray data = ftp.readAllStandardOutput(); auto coutList = data.split('\n'); qDebug() << coutList; }); QObject::connect (&ftp, &QProcess::readyReadStandardError, [&](){ QByteArray data = ftp.readAllStandardError(); auto coutList = data.split('\n'); qDebug() << coutList; }); ftp.start("wget", QStringList() << "-P" << url.toString()); return ftp.waitForFinished();@quadbyte
I don't think OP is asking about retrieving output from the subprocess. If she were there would be no reason to run an xterm to execute thelsusbprocess or whatever command, she would run it directly. As discussed in other threads, you cannot retrieve output from an xterm process as it does not produce any to stdout/err etc., it handles it internally.@AnneRanch
Your issue has nothing to do with multiple parameters, string lists or spaces.Is there a way to verify the actual contents of
QStringList BEFORE running QProcess start?
Of course. Since you put the parameters into a
QStringListvariable you canqDebug() << params;. That tells you precisely what the contents are before you pass toQProcess, no more and no less than that.params << "-e" << "lsusb"; // QProcess starts but terminatesWell yes, because that is what you tell it to do with the
-eargument.xterm -e whateversays: start an xterm, run just and onlywhateverin it and then when that completes exit the xterm. So as soon as yourlsusbcommand finishes, which I guess is microseconds, the xterm process finishes, closes its window, and theQProcessreceives finished and exit code from thexterm. Tested withxterm -e lsit is so quick you don't even see the xterm appear and disappear.You can see this for yourself, nothing to do with Qt or
QProcess. In a terminal just runxterm -e lsusb. You should effectively see nothing (because it's so fast), and you get back to the command prompt indicating that the xterm has exited.If you want to see what is really happening try a long-running command with lots of output:
xterm -e find / -print. See how you see all the output scroll, and when it gets to the end the xterm window closes and exits.I don't know what you intend to achieve via
xterm -e lsusbother than than this behaviour. If you perhaps want the xterm to run the lsusb command and then stay open with its output then ask for this, it would require a different command line to xterm using the-holdoption:// from terminal xterm -hold -e lsusb // from Qt params << "-hold" << "-e" << "lsusb"; QP->start("xterm", params);This will run the
lsusband then hold the xterm window open waiting for you to manually close it. TheQProcesswill not finish until you do so. -
@quadbyte
I don't think OP is asking about retrieving output from the subprocess. If she were there would be no reason to run an xterm to execute thelsusbprocess or whatever command, she would run it directly. As discussed in other threads, you cannot retrieve output from an xterm process as it does not produce any to stdout/err etc., it handles it internally.@AnneRanch
Your issue has nothing to do with multiple parameters, string lists or spaces.Is there a way to verify the actual contents of
QStringList BEFORE running QProcess start?
Of course. Since you put the parameters into a
QStringListvariable you canqDebug() << params;. That tells you precisely what the contents are before you pass toQProcess, no more and no less than that.params << "-e" << "lsusb"; // QProcess starts but terminatesWell yes, because that is what you tell it to do with the
-eargument.xterm -e whateversays: start an xterm, run just and onlywhateverin it and then when that completes exit the xterm. So as soon as yourlsusbcommand finishes, which I guess is microseconds, the xterm process finishes, closes its window, and theQProcessreceives finished and exit code from thexterm. Tested withxterm -e lsit is so quick you don't even see the xterm appear and disappear.You can see this for yourself, nothing to do with Qt or
QProcess. In a terminal just runxterm -e lsusb. You should effectively see nothing (because it's so fast), and you get back to the command prompt indicating that the xterm has exited.If you want to see what is really happening try a long-running command with lots of output:
xterm -e find / -print. See how you see all the output scroll, and when it gets to the end the xterm window closes and exits.I don't know what you intend to achieve via
xterm -e lsusbother than than this behaviour. If you perhaps want the xterm to run the lsusb command and then stay open with its output then ask for this, it would require a different command line to xterm using the-holdoption:// from terminal xterm -hold -e lsusb // from Qt params << "-hold" << "-e" << "lsusb"; QP->start("xterm", params);This will run the
lsusband then hold the xterm window open waiting for you to manually close it. TheQProcesswill not finish until you do so.Partially solved.
It helps to have understanding of QProcess and operator "<<".Or just paying attention what works...
params need to have only single item...
params << "-fa"; params << "Monospace"; params << "-fs"; params <<"16"; -
Partially solved.
It helps to have understanding of QProcess and operator "<<".Or just paying attention what works...
params need to have only single item...
params << "-fa"; params << "Monospace"; params << "-fs"; params <<"16";@AnneRanch said in Passing multiple parameters to QProcess:
params need to have only single item...
Not quite correct.
paramsis aQStringList. A LIST ofQString.
With the<<operator you direct the right side to the left (i.e. the string list).params need to have only single item...
So it's more like "You can add only one after another"
(becauseQProcessneeds every argument as separate string)As you might already know / have seen from @Axel-Spoerl 's suggestion to use
qDebug()This
params << "-fa"; params << "Monospace"; params << "-fs"; params <<"16";produces:
QStringList("-fa")QStringList("-fa", "Monospace")QStringList("-fa", "Monospace", "-fs")QStringList("-fa", "Monospace", "-fs", "16")
Each element is a
QStringand the next to-be-added item is appended to the end of the list.