简体   繁体   中英

Why does widget close instantly?

I'm trying to get a widget created in Qt designer to be called from a menu option from my QMainWindow class in a different script. So far it just exits instantly. I was wondering if anybody could tell me what I'm doing wrong? Do I need to call a subprocess?

Here is the relevant parts of the scripts:

Main Script:

import sys
from PyQt4 import QtGui
from lib import Step1_Import_data

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()

        openEditor = QtGui.QAction("&Editor",self)
        openEditor.setShortcut("Ctrl+E")
        openEditor.triggered.connect(self.editor)

        self.statusBar()
        mainMenu = self.menuBar()
        editorMenu = mainMenu.addMenu('&Editor')
        editorMenu.addAction(openEditor)
        self.showMaximized()

    def editor(self):
        Form = QtGui.QWidget()
        ui = Step1_Import_Data.Ui_Form()
        widget = ui.setupUi(Form)
        Form.show()
        self.setCentralWidget(widget)

def main():
    app = QtGui.QApplication(sys.argv)    
    GUI = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

And then this is my script Step1_Import_Data.py

from PyQt4 import QtCore, QtGui

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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(826, 536)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(680, 10, 131, 41))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        """
        Loads more stuff...
        """

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

   def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "Step 2: Data Checking", None))

        """
        More stuff redacted to be concise
        """

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

You need to keep a reference to the child window, otherwise it will be immediately garbage-collected when the function returns:

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        ...
        self.editorWindow = None            

    def editor(self):
        if self.editorWindow is None:
            self.editorWindow = EditorWindow()
        self.editorWindow.show()

class EditorWindow(QtGui.QWidget):
    def __init__(self):
        super(EditorWindow, self).__init__()
        self.ui = Step1_Import_data.Ui_Form()
        self.ui.setupUi(self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM