Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to delete yourself?
Forum Updated to NodeBB v4.3 + New Features

How to delete yourself?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 490 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nan Feng
    wrote on last edited by
    #1

    Hello, may I ask. I want the Qt program .exe to delete itself. Is there such an API? In addition, how to control the background color of QTextEdit text selection through the style sheet?

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      @Nan-Feng said in How to delete yourself?:

      I want the Qt program .exe to delete itself. Is there such an API?

      The program cannot delete itself, because the file handle will stay used as long as the program runs.

      Anyhow, what you can do is start a script, that will delete the .exe once your application exited. The Installer Framework uses a similar setup to update itself, see e.g. the "deferredRename" function in https://code.qt.io/cgit/installer-framework/installer-framework.git/tree/src/libs/installer/packagemanagercore_p.cpp .

      Director R&D, The Qt Company

      S 1 Reply Last reply
      3
      • kkoehneK kkoehne

        @Nan-Feng said in How to delete yourself?:

        I want the Qt program .exe to delete itself. Is there such an API?

        The program cannot delete itself, because the file handle will stay used as long as the program runs.

        Anyhow, what you can do is start a script, that will delete the .exe once your application exited. The Installer Framework uses a similar setup to update itself, see e.g. the "deferredRename" function in https://code.qt.io/cgit/installer-framework/installer-framework.git/tree/src/libs/installer/packagemanagercore_p.cpp .

        S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        @kkoehne said in How to delete yourself?:

        The program cannot delete itself, because the file handle will stay used as long as the program runs.

        That depends on the OS. It wouldn't work on Windows. Linux, however, allows to change files which are open by someone else. If you delete a file while it is open, the filename is removed from the folder listing and the file is actually deleted when the last file handle is closed. @Nan-Feng Please let us know which operating system you use to figure out if it is even possible.

        You can use QCoreApplication::applicationFilePath() to figure out the filename of your .exe. QFile has a static function to delete files. However, this would not work on Windows.

        One trick for Windows could be that you have a separate .exe which just deletes a file provided by a command line argument (maybe have it wait a few seconds or try multiple times in loop to delete the file). Put this .exe as resource into your .exe. When you want to delete yourself, put a copy of the deleter .exe into a temporary path and call is from your .exe (using QProcess) and quit your own .exe. The deleter .exe should then be able to delete the original .exe file. You cannot delete your deleter .exe. However, if you place it in the temporary folder, Windows will delete it eventually.

        artwawA 1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          I hope nobody will mind now if I nominate this thread for the best ever title! ;-)
          It intrigued me when I first saw it....

          jsulmJ 1 Reply Last reply
          3
          • S SimonSchroeder

            @kkoehne said in How to delete yourself?:

            The program cannot delete itself, because the file handle will stay used as long as the program runs.

            That depends on the OS. It wouldn't work on Windows. Linux, however, allows to change files which are open by someone else. If you delete a file while it is open, the filename is removed from the folder listing and the file is actually deleted when the last file handle is closed. @Nan-Feng Please let us know which operating system you use to figure out if it is even possible.

            You can use QCoreApplication::applicationFilePath() to figure out the filename of your .exe. QFile has a static function to delete files. However, this would not work on Windows.

            One trick for Windows could be that you have a separate .exe which just deletes a file provided by a command line argument (maybe have it wait a few seconds or try multiple times in loop to delete the file). Put this .exe as resource into your .exe. When you want to delete yourself, put a copy of the deleter .exe into a temporary path and call is from your .exe (using QProcess) and quit your own .exe. The deleter .exe should then be able to delete the original .exe file. You cannot delete your deleter .exe. However, if you place it in the temporary folder, Windows will delete it eventually.

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @SimonSchroeder But would not (on Windows) suffice to QProcess a routine (detached) that would erase the .exe once it stopped? I do remember writing self updater once that operated on the same principle: main thread was downloading the zip, start detached process, then detached process was unpacking the zip overwriting the main exe and all updated files, then starting the exe back and terminating self. It was working on Win7 and Win10 (last time I checked the company that was using this software of mine was in 2019 and it was working then).

            For more information please re-read.

            Kind Regards,
            Artur

            JonBJ 1 Reply Last reply
            0
            • JonBJ JonB

              I hope nobody will mind now if I nominate this thread for the best ever title! ;-)
              It intrigued me when I first saw it....

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB Be careful if you delete something, you could delete yourself :-)

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • artwawA artwaw

                @SimonSchroeder But would not (on Windows) suffice to QProcess a routine (detached) that would erase the .exe once it stopped? I do remember writing self updater once that operated on the same principle: main thread was downloading the zip, start detached process, then detached process was unpacking the zip overwriting the main exe and all updated files, then starting the exe back and terminating self. It was working on Win7 and Win10 (last time I checked the company that was using this software of mine was in 2019 and it was working then).

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @artwaw
                Under Linux or Windows, if the process has terminated it should be deletable. If a parent fork-execs (I don't know if exec without fork would also allow this, suspect it would) a child and does not wait for it, the child should be able to delete the parent file once the parent has exited.

                artwawA 1 Reply Last reply
                1
                • JonBJ JonB

                  @artwaw
                  Under Linux or Windows, if the process has terminated it should be deletable. If a parent fork-execs (I don't know if exec without fork would also allow this, suspect it would) a child and does not wait for it, the child should be able to delete the parent file once the parent has exited.

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #8

                  @JonB exactly my point. I was referring to Windows as this was the only case I covered in practice.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved