Why does the example below only work if the useless
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
_ = Test()
That's a very good question and I've faced a lot of weird problems in the past because of this fact with my PyQt widgets and plugins, basically that happens thanks to the python garbage collector.
When you assign your instance to that
_ dummy variable before entering the Qt's main loop, there will be a living reference that will avoid to be collected by the garbage collector, therefore the widget won't be destroyed.