Is is possible to create and use new QApplication instance after previous one has been exited?
-
2Did you try with simple case ? I did and I am definitely able to do so. I use this to run test apps successively ... So I would say, yes. – Thomas Vincent Jul 21 '11 at 15:24
3 Answers
Yes, you can create a new QApplication after the previous instance is destroyed. I verified this in Windows using PyQt4. The program below displays an empty windows. Upon closing the first window the first QApplication is destroyed and a second QApplication is created that then shows a second blank window. Note that I had problems without the del app
statement. This would be equivalent to using delete
on your QApplication in C++. Just make sure to allocate the QApplication instance on the heap instead of the stack.
from PyQt4 import QtCore, QtGui
import sys
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.show()
app.exec_()
del app # force garbage collection of the first QApplication
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.show()
app.exec_()

- 26,961
- 9
- 82
- 99
-
Is there a way I can render multiople URLs like this? http://blog.sitescraper.net/2010/06/scraping-javascript-webpages-in-python.html – Shane Reustle Aug 04 '11 at 20:39
-
That's a bit off topic. However, there is no need for multiple QApplication instances. Just create multiple web views. – Judge Maygarden Aug 05 '11 at 14:16
Looks like this question discusses that:
Problems with Multiple QApplications
Instead of creating a new instance of QApplication, you could create a new thread with its own window, and treat that as you would of a different QApplication from within the single program.

- 1
- 1
-
2I believe the other question has a subtle difference. It sounds like mgamer wants to only have one QApplication active at a time, but create a new one after the previous one is destroyed. – Judge Maygarden Jul 21 '11 at 15:30
-
For who google for Python
, This is worked with me fine
if __name__ == "__main__":
# Create a singleinstance of QApplication
app = QApplication.instance()
if not app:
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

- 403
- 4
- 10