I handle the normal copying of files with Qt like this:
QFile::copy("/path/file", "/path/copy-of-file");
How can I now copy a file for which Sudo rights are required.
I handle the normal copying of files with Qt like this:
QFile::copy("/path/file", "/path/copy-of-file");
How can I now copy a file for which Sudo rights are required.
You can use QProcess and pkexec to execute a command as another user
pkexec allows an authorized user to execute PROGRAM as another user. If username is not specified, then the program will be executed as the administrative super user, root.
https://www.freedesktop.org/software/polkit/docs/0.105/pkexec.1.html
QProcess *proc = new QProcess(this);
proc->waitForFinished();
QString cmd = "pkexec /bin/cp /path/file /path/copy-of-file";
proc->start(cmd);
if(!proc->waitForStarted()) //default wait time 30 sec
{
qDebug() << "Cannot execute:" << cmd;
}
proc->waitForFinished();
proc->setProcessChannelMode(QProcess::MergedChannels);
if(proc->exitStatus() == QProcess::NormalExit
&& proc->exitCode() == QProcess::NormalExit){
qDebug() << "Success";
} else {
qDebug() << "Cannot copy file" << cmd;
}
run shell command, like this
sudo cp /path/file /path/copy-of-file
Set a password if necessary.:
$echo <password> | sudo -S <command>