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 484 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 13 Jun 2021, 11:06 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
    • K Offline
      K Offline
      kkoehne
      Moderators
      wrote on 13 Jun 2021, 12:07 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 15 Jun 2021, 12:33
      3
      • K kkoehne
        13 Jun 2021, 12:07

        @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 15 Jun 2021, 12:33 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.

        A 1 Reply Last reply 15 Jun 2021, 12:45
        0
        • J Online
          J Online
          JonB
          wrote on 15 Jun 2021, 12:41 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....

          J 1 Reply Last reply 15 Jun 2021, 12:56
          3
          • S SimonSchroeder
            15 Jun 2021, 12:33

            @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.

            A Offline
            A Offline
            artwaw
            wrote on 15 Jun 2021, 12:45 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

            J 1 Reply Last reply 15 Jun 2021, 13:31
            0
            • J JonB
              15 Jun 2021, 12:41

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Jun 2021, 12:56 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
              • A artwaw
                15 Jun 2021, 12:45

                @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).

                J Online
                J Online
                JonB
                wrote on 15 Jun 2021, 13:31 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.

                A 1 Reply Last reply 15 Jun 2021, 13:32
                1
                • J JonB
                  15 Jun 2021, 13:31

                  @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.

                  A Offline
                  A Offline
                  artwaw
                  wrote on 15 Jun 2021, 13:32 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

                  1/8

                  13 Jun 2021, 11:06

                  • Login

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