Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to destroy a singleton and then create a new one.
QtWS25 Last Chance

How to destroy a singleton and then create a new one.

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2pyside
11 Posts 8 Posters 13.3k Views
  • 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.
  • A Offline
    A Offline
    a_fol
    wrote on 7 Jan 2020, 10:32 last edited by a_fol 1 Jul 2020, 11:13
    #1

    I want to create a qapplication to record a sound. Then I want to save the audio file, run some functions on that audio file and then I want to create a new qapplication which will display the results of that functions. Right now I can only get the first qapplication and the functions to run. The script fails before the second application with this error:

    RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

    def main(audio_file, a_file, final_order):
    
        app = qtw.QApplication(sys.argv)
        w1=record_audio.main()
        w1.show()
        app.exec_()
    
        r = sr.Recognizer()
        audio = SpeechConvert(audio_file, r)
        a = get_a.main(a_file)
        get_command.main(audio, r, a)
    
        app = qtw.QApplication(sys.argv)
        w2=display_qt.main(final_order)
        w2.show()
        sys.exit(app.exec_())
    
        #w1.closed.connect(w2.show)
        #sys.exit(app.exec_())
    
    if __name__ == "__main__":
        audio_file = filenames.audio_file2
        a_file = filenames.a_file
        final_order = filenames.final_order
        main(audio_file, a_file, final_order)
    
    J J 2 Replies Last reply 7 Jan 2020, 11:00
    0
    • A a_fol
      7 Jan 2020, 10:32

      I want to create a qapplication to record a sound. Then I want to save the audio file, run some functions on that audio file and then I want to create a new qapplication which will display the results of that functions. Right now I can only get the first qapplication and the functions to run. The script fails before the second application with this error:

      RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

      def main(audio_file, a_file, final_order):
      
          app = qtw.QApplication(sys.argv)
          w1=record_audio.main()
          w1.show()
          app.exec_()
      
          r = sr.Recognizer()
          audio = SpeechConvert(audio_file, r)
          a = get_a.main(a_file)
          get_command.main(audio, r, a)
      
          app = qtw.QApplication(sys.argv)
          w2=display_qt.main(final_order)
          w2.show()
          sys.exit(app.exec_())
      
          #w1.closed.connect(w2.show)
          #sys.exit(app.exec_())
      
      if __name__ == "__main__":
          audio_file = filenames.audio_file2
          a_file = filenames.a_file
          final_order = filenames.final_order
          main(audio_file, a_file, final_order)
      
      J Offline
      J Offline
      JonB
      wrote on 7 Jan 2020, 11:00 last edited by JonB 1 Jul 2020, 11:00
      #2

      @a_fol
      Well, it seems to be telling you to go del app before you can go app = qtw.QApplication(sys.argv) a second time. The answer at https://stackoverflow.com/a/6778997/489865 seems to be you (it says PyQt4, let's hope it applies to PyQt5 too).

      Having said that, I do not know whether you ought also exit the exiting app "cleanly" from a Qt point of view. I don't know why one would want to destroy an existing QApplication and then recreate, but that's up to you.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        a_fol
        wrote on 7 Jan 2020, 11:20 last edited by
        #3

        Thanks for your reply! I tried adding del app afte the first app.exec_() but I still get the same error about destroying the QApplication singleton before creating a new instance.

        What I want is similar to this:
        https://stackoverflow.com/questions/59581668/how-to-destroy-a-qapplication-and-then-run-a-new-one-without-exiting-the-python

        But instead I can't have the two applications connected to each other as that prevents the functions between them to run in a sequence. So I can't think of another way other than destroying the first one and creating a new one after the functions in between are done.

        1 Reply Last reply
        0
        • A a_fol
          7 Jan 2020, 10:32

          I want to create a qapplication to record a sound. Then I want to save the audio file, run some functions on that audio file and then I want to create a new qapplication which will display the results of that functions. Right now I can only get the first qapplication and the functions to run. The script fails before the second application with this error:

          RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

          def main(audio_file, a_file, final_order):
          
              app = qtw.QApplication(sys.argv)
              w1=record_audio.main()
              w1.show()
              app.exec_()
          
              r = sr.Recognizer()
              audio = SpeechConvert(audio_file, r)
              a = get_a.main(a_file)
              get_command.main(audio, r, a)
          
              app = qtw.QApplication(sys.argv)
              w2=display_qt.main(final_order)
              w2.show()
              sys.exit(app.exec_())
          
              #w1.closed.connect(w2.show)
              #sys.exit(app.exec_())
          
          if __name__ == "__main__":
              audio_file = filenames.audio_file2
              a_file = filenames.a_file
              final_order = filenames.final_order
              main(audio_file, a_file, final_order)
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Jan 2020, 11:37 last edited by
          #4

          @a_fol
          let me ask this, why do you want to destroy the QApplication to then immediately create a new one!?

          What's stopping you from doing everything inside one QApplication instance?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • A Offline
            A Offline
            a_fol
            wrote on 7 Jan 2020, 11:57 last edited by
            #5

            @a_fol said in How to destroy a singleton and then create a new one.:

            RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

            This error:

            RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

            If there is a way having both windows displayed in the sequence mentioned above, that is:

            1. w1
            2. Recognizer, SpeechConvert etc functions
            3. w2
              then I that would be great. It's just that looking at the error I thought destroying w1 and then creating w2 would solve everything.
            jsulmJ 1 Reply Last reply 7 Jan 2020, 12:02
            0
            • A a_fol
              7 Jan 2020, 11:57

              @a_fol said in How to destroy a singleton and then create a new one.:

              RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

              This error:

              RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

              If there is a way having both windows displayed in the sequence mentioned above, that is:

              1. w1
              2. Recognizer, SpeechConvert etc functions
              3. w2
                then I that would be great. It's just that looking at the error I thought destroying w1 and then creating w2 would solve everything.
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 7 Jan 2020, 12:02 last edited by
              #6

              @a_fol There is really no need to recreate QApplication to show two windows in a sequence!
              First show the first window. When it is closed create and show the second one.

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

              1 Reply Last reply
              2
              • C Offline
                C Offline
                canol
                wrote on 7 Jan 2020, 12:33 last edited by
                #7

                Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                If I continue here; my problem is the same thing (I cannot create a new QApplication after destroying one) and my application is not a singleton, so users are able to quit the application and create a new one in their programs. I can make it a singleton, and maybe I should, since a process cannot have more than one QApplication running at the same time as far as I know.

                But I still wonder why this code does not work, and if this is a bug? If yes, I'll look out for a way to report it. By the way the code works without a problem on PyQt5, it gives the singleton error only with PySide2.

                For reference the code I try:

                import sys
                from PySide2.QtWidgets import *
                
                app = QApplication(sys.argv)
                label = QLabel("Hello World")
                label.show()
                app.exec_()
                
                del app
                
                app = QApplication(sys.argv)
                label = QLabel("Hello World")
                label.show()
                app.exec_()
                
                sys.exit()
                
                Pablo J. RoginaP J S 3 Replies Last reply 7 Jan 2020, 12:53
                0
                • C canol
                  7 Jan 2020, 12:33

                  Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                  If I continue here; my problem is the same thing (I cannot create a new QApplication after destroying one) and my application is not a singleton, so users are able to quit the application and create a new one in their programs. I can make it a singleton, and maybe I should, since a process cannot have more than one QApplication running at the same time as far as I know.

                  But I still wonder why this code does not work, and if this is a bug? If yes, I'll look out for a way to report it. By the way the code works without a problem on PyQt5, it gives the singleton error only with PySide2.

                  For reference the code I try:

                  import sys
                  from PySide2.QtWidgets import *
                  
                  app = QApplication(sys.argv)
                  label = QLabel("Hello World")
                  label.show()
                  app.exec_()
                  
                  del app
                  
                  app = QApplication(sys.argv)
                  label = QLabel("Hello World")
                  label.show()
                  app.exec_()
                  
                  sys.exit()
                  
                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 7 Jan 2020, 12:53 last edited by
                  #8

                  @canol said in How to destroy a singleton and then create a new one.:

                  Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                  Please don't double post!!

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  C 1 Reply Last reply 7 Jan 2020, 16:18
                  0
                  • C canol
                    7 Jan 2020, 12:33

                    Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                    If I continue here; my problem is the same thing (I cannot create a new QApplication after destroying one) and my application is not a singleton, so users are able to quit the application and create a new one in their programs. I can make it a singleton, and maybe I should, since a process cannot have more than one QApplication running at the same time as far as I know.

                    But I still wonder why this code does not work, and if this is a bug? If yes, I'll look out for a way to report it. By the way the code works without a problem on PyQt5, it gives the singleton error only with PySide2.

                    For reference the code I try:

                    import sys
                    from PySide2.QtWidgets import *
                    
                    app = QApplication(sys.argv)
                    label = QLabel("Hello World")
                    label.show()
                    app.exec_()
                    
                    del app
                    
                    app = QApplication(sys.argv)
                    label = QLabel("Hello World")
                    label.show()
                    app.exec_()
                    
                    sys.exit()
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 7 Jan 2020, 14:50 last edited by
                    #9

                    @canol
                    I was the person who replied to you in your other thread to come here to this one!

                    As per my observation there, your findings imply that there is a difference between PyQt5 & PySide2 here. Whether it;s a "bug" or "different handling" I can't say. It looks like the behaviour of del app varies. Not sure what to tell you to do if you insist on multiple QApplications, because the only answers are for PyQt5. You might try "shutting down" your first QApplication better, but I'm not certain how.....

                    1 Reply Last reply
                    1
                    • Pablo J. RoginaP Pablo J. Rogina
                      7 Jan 2020, 12:53

                      @canol said in How to destroy a singleton and then create a new one.:

                      Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                      Please don't double post!!

                      C Offline
                      C Offline
                      canol
                      wrote on 7 Jan 2020, 16:18 last edited by
                      #10

                      @Pablo-J-Rogina said in How to destroy a singleton and then create a new one.:

                      @canol said in How to destroy a singleton and then create a new one.:

                      Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                      Please don't double post!!

                      I did not double post, we created similar question almost at the same time, I did not see this poster's post before I created mine. Do you have anything useful to add to the discussion?

                      @JonB said in How to destroy a singleton and then create a new one.:

                      @canol
                      I was the person who replied to you in your other thread to come here to this one!

                      As per my observation there, your findings imply that there is a difference between PyQt5 & PySide2 here. Whether it;s a "bug" or "different handling" I can't say. It looks like the behaviour of del app varies. Not sure what to tell you to do if you insist on multiple QApplications, because the only answers are for PyQt5. You might try "shutting down" your first QApplication better, but I'm not certain how.....

                      Thank you, I'll investigate further.

                      1 Reply Last reply
                      1
                      • C canol
                        7 Jan 2020, 12:33

                        Hello, interestingly, I created almost the same question about the same time in another thread: https://forum.qt.io/topic/110416/pyside2-destroying-the-qapplication-instance-and-creating-another-one-gives-an-error

                        If I continue here; my problem is the same thing (I cannot create a new QApplication after destroying one) and my application is not a singleton, so users are able to quit the application and create a new one in their programs. I can make it a singleton, and maybe I should, since a process cannot have more than one QApplication running at the same time as far as I know.

                        But I still wonder why this code does not work, and if this is a bug? If yes, I'll look out for a way to report it. By the way the code works without a problem on PyQt5, it gives the singleton error only with PySide2.

                        For reference the code I try:

                        import sys
                        from PySide2.QtWidgets import *
                        
                        app = QApplication(sys.argv)
                        label = QLabel("Hello World")
                        label.show()
                        app.exec_()
                        
                        del app
                        
                        app = QApplication(sys.argv)
                        label = QLabel("Hello World")
                        label.show()
                        app.exec_()
                        
                        sys.exit()
                        
                        S Offline
                        S Offline
                        sergio bartolini
                        wrote on 1 Feb 2020, 11:39 last edited by sergio bartolini 2 Jan 2020, 11:39
                        #11

                        @canol , I've met same issue in Spyder (debug is possible one time after start only).
                        So i've found a workaround:

                            app = QApplication.instance()
                            if app == None:
                                app = QApplication([])
                        

                        instead of usual

                            app = QApplication([])
                        

                        Maybe it'll help for you. It did the trick for me.

                        1 Reply Last reply
                        2

                        • Login

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