How to launch a QProcess with root rights?

2019-02-11 05:31发布

问题:

I need to launch gphoto2 from a Qt program. I do this:

QString gphotoProgram = "/usr/bin/gphoto2";
QStringList gphotoArguments;
gphotoArguments << "--capture-image";
QProcess *gphotoProcess = new QProcess(this);
gphotoProcess->start(gphotoProgram, gphotoArguments);

but it never enters the Running state this way, as gphoto2 usually needs admin rights to be launched on command line.

How can I start this QProcess with proper rights to make gphoto2 working?

Edit: I precise that I would prefer the user to not have to enter a password, which means gksudo, kdesudo or any other graphical solution is not a valid option for me.

回答1:

I would strongly recommend finding a way to allow gphoto2 to be run with the logged in user's permissions. Perhaps this article has some helpful info.



回答2:

If you have a distribution with sudo enabled, try to add "gksudo" to the command line of your process:

QString gphotoProgram = "gksudo /usr/bin/gphoto2"

If the user account is authorized as a sudo-er, it will ask the user password so that the program can run with root rights.



回答3:

You can also use PolicyKit to start QProcess with sudo rights.

pkexec command

QString gphotoProgram = "pkexec /usr/bin/gphoto2";



回答4:

Don't GNOME and KDE still have their own graphical sudo wrappers? (I'm a Windows guy myself.) You could use QProcess to launch "sudo" and let it take care of the elevation and subsequent gphoto launch.