How to destroy a singleton and then create a new one.
-
@a_fol
Well, it seems to be telling you to godel app
before you can goapp = 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 existingQApplication
and then recreate, but that's up to you. -
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-pythonBut 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.
-
@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:
- w1
- Recognizer, SpeechConvert etc functions
- 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.
-
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()
-
@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!!
-
@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 multipleQApplications
, because the only answers are for PyQt5. You might try "shutting down" your firstQApplication
better, but I'm not certain how..... -
@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 multipleQApplications
, because the only answers are for PyQt5. You might try "shutting down" your firstQApplication
better, but I'm not certain how.....Thank you, I'll investigate further.
-
@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.