Recreate QApplication after previous instance has been exited
开发者_高级运维Is is possible to create and use new QApplication instance after previous one has been exited?
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_()
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.
精彩评论