1

How can I parse a parameter like --message "text" to /usr/bin/gksudo using QProcess to show my individualized text?

Just with /usr/bin/gksudo and calling my script.sh it works fine.

Here the minimal example:

QString cmd = QString("/usr/bin/gksudo");
QStringList param = ( QStringList << "--message my Text" << "path/to/script.sh")

QProcess.start( cmd, param );

Even if i try to add the parameter to the cmd i fail. And no password prompt is shown.

QString cmd = QString("/usr/bin/gksudo --message MyText");
imizeropt
  • 176
  • 1
  • 13

2 Answers2

1

Solution

--message and my Text are both own elements.

QStringList param = ( QStringList << "--message" << tr("my Text") << "path/to/script.sh")
imizeropt
  • 176
  • 1
  • 13
0

QProcess takes the first parameter as the command to run and then passes the following arguments, delimited by a space, as arguments to the command.

When you do this: -

QStringList param = ( QStringList << "--message my Text" << "path/to/script.sh")

And then pass param to QProcess, it's passing "path/to/script.sh" as a command line parameter to gksudo, but what you want is a single argument with --message. You need to unify the parameters with extra quotes. So, in the case of your last example, that would be: -

QString cmd = QString("/usr/bin/gksudo \"--message MyText"\");

Note the two additional \" around --message MyText

Passing this to QProcess means there are two arguments; the call to gksudo and its command line argument "--message MyText"

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • I’d try with `”—message” << “my text” << “/path/to/script.sh”` first – Frank Osterfeld Feb 21 '14 at 12:15
  • @FrankOsterfeld, that would produce the same problem. If using a QStringList, you still need a single argument to gksudo, so it would need to be << "-message -text \"/path/to/script.sh\"" as "my text" and "/path/to/script.sh" are not valid arguments to pass to gksudo on their own. – TheDarkKnight Feb 21 '14 at 14:41