Problems with providing arguments to QProcess
-
I am trying to execute a c program from a QT application. I use the following code
for the same but it does not work.QProcess *process = new QProcess(); QString program = "sh"; QString str1 = "gcc"; QString str2 = "/home/Desktop/QtTest/basic.c"; QString res = str1+ " " + str2; QStringList arguments; arguments << "-c" << res; process->start(program,arguments); if (!process->waitForStarted()) { qDebug("error in start"); return false; } process->closeWriteChannel(); if (!process->waitForFinished()) { qDebug("error in finnished"); return false; }
But if I use --help as argument instead of filepath the command is executed successfully.
QProcess *process = new QProcess(); QString program = "sh"; QString str1 = "gcc"; QString str2 = "--help"; QString res = str1+ " " + str2; QStringList arguments; arguments << "-c" << res; process->start(program,arguments); if (!process->waitForStarted()) { qDebug("error in start"); return false; } process->closeWriteChannel(); if (!process->waitForFinished()) { qDebug("error in finnished"); return false; }
I am not able to understand why such behaviour and what is wrong on my side.
As I know, I am missing something here. -
First - why do you execute sh instead gcc directly?
Second - the arguments should be passed as separate arguments instead manually concatenated - otherwise they are not recognized as two separate arguments. -
I tried executing gcc directly, but I am facing the same problem as asked previously.
QProcess process = new QProcess(); QString program = "gcc"; QStringList arguments; arguments << "--help";// "/home/sanket/Desktop/QtTest/basic.c"; process->start(program,arguments);
the gcc --help command is executed, but gcc along with the commented line as argument returns an empty string for readAllStandardOutput and readAllStandardError functions.
- How can I pass separate arguments to start function as it takes two arguments one as QString and other as QStringList
-
Hi
First of all. if gcc can just compile the file. nothing is shown. no output at all.
Fooled me at first.This code works for me and show any syntax errors if i introduce one.
#include <QMessageBox> #include <QProcess> int main(int argc, char* argv[]) { QApplication a(argc, argv); QProcess process; QString program = "gcc"; QStringList arguments; process.setProcessChannelMode(QProcess::MergedChannels); arguments << "-Wall" << "/home/master/test.cpp" << "-o" << "hello"; process.start(program, arguments); process.waitForFinished(); // sets current thread to sleep and waits for process end ( lacks check) QString output(process.readAllStandardOutput()); QMessageBox::warning(0, "output", output ); MainWindow w; w.show(); return a.exec(); }
test.cpp is
#include <stdio.h> ddd int main (void) { printf ("Hello, world!\n"); return 0; }
and this is what sample says:
-
Hi
Is there a way to use the below command in qProcess
systeminfo | findstr /C:"OS Name"
I tried to use it in this wayQProcess m_process; QString OSCommand = "systeminfo | findstr /C:'OS Name'"; m_process.start(OSCommand); m_process.waitForFinished();
This didn't work,
How can we handle these double quotes in our QString? -
@LusuQT said in Problems with providing arguments to QProcess:
How can we handle these double quotes in our QString?
By escaping them:
QString OSCommand = "systeminfo | findstr /C:\"OS Name\"";
-
@LusuQT
Be aware of a couple of things with that command.The
QProcess::start()
overload which accepts just a string (program + arguments) as a parameter has been removed. Though aQProcess::startCommand(const QString &command, QIODeviceBase::OpenMode mode = ReadWrite)
has been added at Qt 6.0.QProcess::start(const QString &program, const QStringList &arguments = {}, QIODeviceBase::OpenMode mode = ReadWrite)
, orQProcess::execute(const QString &program, const QStringList &arguments = {})
, takes arguments as a list.Because your command uses a *shell redirection token", the
|
character, this command string must be passed to the shell (cmd
) for interpretation, else it won't work.Your command should be:
m_process.start("cmd", { "/c", OSCommand } );
Since you are going to have read the output from this command (presumably, else there is no point in issuing it), in this case you might save yourself some time/hassle if all you do is run the simple
m_process.start("systeminfo");
and do you own searching for string
OS Name
as you read. That would save the extra process, the|
piping and the need to usecmd /c
. -
Hi,
There is a simpler method: setStandardOutputProcess.
Use one QProcess for each element of your pipe. That way you recreate your chain in code and no dependency on shell handling.