PySide issue with keeping a window up

The window I am showing doesn’t stay visible in Maya and just poofs. Here’s my code:

from PySide import QtCore, QtGui, QtUiTools
import maya.cmds as cmds

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
           
def launch():
    import sys
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)       
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    app.exec_()

launch()

The strange thing that I am not understanding is when I had all of the code from launch() in an if statement for main, the window shows and persists. However moving this code to a function to be called causes the issue.

This worked until I moved it:

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)       
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    app.exec_()

Shouldn’t the app.exec be holding the window? I don’t get this.

its a dialog, use dialog.exec_() to hold it open. If you want a non-modal window, use QMainWindow and then you can use app.exec_()