How to run print command lpr -p programmatically t

2019-05-31 14:03发布

I want to run print command lpr -p programmatically through root privilege in Qt. Actually I want to print the pdf file using these command. This command is working through terminal but not through programmatically.

Thanks in advance.

1条回答
劳资没心,怎么记你
2楼-- · 2019-05-31 14:44

you can run commands that need root privilege by running :

echo myPass | sudo -S lpr -p

Although it's not a good idea to echo your password in shell but you can do it in Qt via Qprocess like :

QProcess process1;
QProcess process2;

process1.setStandardOutputProcess(&process2);

process1.start("echo myPass");
process2.start("sudo -S lpr -p");
process2.setProcessChannelMode(QProcess::ForwardedChannels);


process2.waitForFinished(3000);
查看更多
登录 后发表回答