linuxqtubuntugksudo

QProcess call gksudo with parameter for individualized message for calling script


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");

Solution

  • 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"