Update own Qt application

1.2k views Asked by At

I am working on linux. I wrote a small Qt application that itself should install the new version of application (software update). and On click of restart button from Ui should restart with updated version.

For that, I am applying the below logic :

QDir sourceDir("/Desktop/FileTransferDemo");
QDir currentDir(QDir::currentPath());
QProcess copying;
QStringList arg;
arg.append(sourceDir.filePath("CopyAndRestore_v2"));
arg.append(currentDir.path());

copying.start("cp",arg);
if (copying.waitForFinished(12000) == true)
{
    if (copying.exitCode() == 0)
    {
    }
}

QFile app(currentDir.filePath("CopyAndRestore"));
app.remove();
QFile newApp(currentDir.filePath("CopyAndRestore_v2"));
newApp.rename("CopyAndRestore");

i.e . copying new exe at the same path remove old exe and rename the new. here CopyAndRestore is my old exe and CopyAndRestore_v2 is the new one. After renaming the new exe and on click of restart button with below code:

on_restartButton_clicked()
{
    QProcess::startDetached(QCoreApplication::applicationFilePath());
    qApp->quit();
}

I am expecting the application should restart with new version. Bt it is not restarting. is this approach correct? Please suggest the solution for the expected output.

1

There are 1 answers

0
akw On

I would suggest the following procedure:

  • User starts mytool[.exe]
  • mytool[.exe] downloads the update, validates it and decrompresses it to mytool_new[.exe]
  • Make it executable (QFile::setPermissions(..) like chmod 755)
  • Run the new program with QProcess and terminate.
  • The new process checks its name (args[0]) to determine its intermediate state. Then it copies itself to the original location.
  • New process runs itself from the original location with QProcess and terminates.

-> You are running an updated version of the file.

(I think the Qt Maintenance Tool does it exactly like that)