如何以 root 权限启动 QProcess?
我需要从 Qt 程序启动 gphoto2
。我这样做:
QString gphotoProgram = "/usr/bin/gphoto2";
QStringList gphotoArguments;
gphotoArguments << "--capture-image";
QProcess *gphotoProcess = new QProcess(this);
gphotoProcess->start(gphotoProgram, gphotoArguments);
但它永远不会以这种方式进入 Running
状态,因为 gphoto2
通常需要管理员权限才能在命令行上启动。
如何以适当的权限启动此 QProcess
以使 gphoto2
正常工作?
编辑:我明确指出,我希望用户不必输入密码,这意味着 gksudo、kdesudo 或任何其他图形解决方案对我来说都不是有效的选项。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我强烈建议找到一种方法来允许 gphoto2 在登录用户的权限下运行。也许这篇文章 有一些有用的信息。
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.
如果您的发行版启用了 sudo,请尝试将“gksudo”添加到进程的命令行中:
如果用户帐户被授权为 sudo-er,它将询问用户密码,以便程序可以使用 root 权限运行。
If you have a distribution with sudo enabled, try to add "gksudo" to the command line of your process:
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.
您还可以使用PolicyKit以sudo权限启动QProcess。
pkexec 命令
QString gphotoProgram = "pkexec /usr/bin/gphoto2";
You can also use PolicyKit to start QProcess with sudo rights.
pkexec command
QString gphotoProgram = "pkexec /usr/bin/gphoto2";
GNOME 和 KDE 是否仍然有自己的图形 sudo 包装器? (我自己就是 Windows 用户。)您可以使用 QProcess 启动“sudo”并让它处理海拔和随后的 gphoto 启动。
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.