PyQt4:打开并正确销毁另一个QMainWindow

时间:2016-10-12 07:51:54

标签: python pyqt4 qmainwindow

我的项目有问题。在其中我有一个主要表单,有八个巨大的按钮来调用其他表单。基本上我用Qt Designer创建了ui文件,并使用pyuic4 [ui_file] -o [py_file]方法创建表单,并在main.py我将它们全部导入。

以下是代码的一部分:

from myraflib import myEnv
from myraflib import myFit

import sys
import sip
for cl in ('QString', 'QVariant'):
    sip.setapi(cl, 2)

from uis.myraf import Ui_MainWindow
from uis.calibration import Ui_MainWindowCali
from uis.align import Ui_MainWindowAlign
from uis.photometry import Ui_MainWindowPhoto
from uis.graph import Ui_MainWindowGraph
from uis.editor import Ui_MainWindowEdit
from uis.scheduler import Ui_MainWindowSche
from uis.settings import Ui_MainWindowSett
from uis.help import Ui_MainWindowHelp
from uis.error import Ui_MainWindowErr

from PyQt4 import QtGui
from PyQt4 import QtCore
import gui as g
import functions as f

from fPlot import *


class MyForm(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, verb=True):
        super(MyForm, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.verb = verb
        self.ggui = g.Gui(verb=self.verb)
        self.meetc = myEnv.etc(verb=self.verb)

        self.child = {"calib": None, "align": None, "photo": None,
            "graph": None, "editor": None, "sched": None, "setti": None,
            "help": None}

        ...

        self.ui.pushButton.clicked.connect(lambda: self.show_form("calib"))

        self.ui.pushButton_2.clicked.connect(lambda: self.show_form("align"))

        self.ui.pushButton_3.clicked.connect(lambda: self.show_form("photo"))

        self.ui.pushButton_4.clicked.connect(lambda: self.show_form("graph"))

        self.ui.pushButton_5.clicked.connect(lambda: self.show_form("editor"))

        self.ui.pushButton_6.clicked.connect(lambda: self.show_form("sched"))

        self.ui.pushButton_7.clicked.connect(lambda: self.show_form("setti"))

        self.ui.pushButton_8.clicked.connect(lambda: self.show_form("help"))

        self.ui.actionQuit.triggered.connect(QtGui.qApp.quit)

    #Here my best friend. The problem guy is here
    def show_form(self, frm):
        try:
            forms = {"calib": MyFormCali(self), "align": MyFormAli(self),
                 "photo": MyFormPho(self), "graph": MyFormGra(self),
                 "editor": MyFormEdi(self), "sched": MyFormSch(self),
                 "setti": MyFormSet(self), "help": MyFormHel(self)}
            if self.child[frm] is None:
                self.child[frm] = forms[frm]
            self.child[frm].show()
        except Exception as e:
            self.meetc.print_if(e)

    ...

    def closeEvent(self, evnt):
        for i in self.child:
            if not self.child[i] is None:
                self.child[i].close()


class MyFormCali(QtGui.QMainWindow, Ui_MainWindowCali):
    def __init__(self, parent=None, verb=True):
        super(MyFormCali, self).__init__()
        self.ui = Ui_MainWindowCali()
        self.ui.setupUi(self)
        self.verb = verb
        self.ggui = g.Gui(verb=self.verb)
        self.meetc = myEnv.etc(verb=self.verb)
        self.mefo = myEnv.file_operation(verb=self.verb)
        self.mffo = myFit.fits_operation(verb=self.verb)
        self.mfho = myFit.header_operation(verb=self.verb)
        self.mfst = myFit.statistics_operation(verb=self.verb)
        self.fset = f.settings(verb=self.verb)

        ...

        self.child = {"error": None}

        ...

        self.ui.pushButton_11.clicked.connect(lambda:
            self.add_files(self.ui.listWidget_5))
        self.ui.pushButton_13.clicked.connect(lambda:
            self.add_files(self.ui.listWidget_6))
        self.ui.pushButton_15.clicked.connect(lambda:
            self.add_files(self.ui.listWidget_7))
        self.ui.pushButton_17.clicked.connect(lambda:
            self.add_files(self.ui.listWidget_8))

        ...

        self.info_update()

    ...

    def show_stats(self, list_dev):
        file_name = self.ggui.get_current_item_from_list(list_dev)

        ...

    def info_update(self):
        cda = 0

        ...

    def add_files(self, list_widget):
        files = self.ggui.get_file_list(self)
        if files[0]:
            self.ggui.set_values_to_list(self, list_widget, files[1])
        else:
            self.ggui.show_dialog(self, "info",
                "File Problem", "No file selected!")

        self.info_update()

    def closeEvent(self, evnt):
        for i in self.child:
            if not self.child[i] is None:
                self.child[i].close()

嗯,我可以分享整个项目,但总体思路是至少有九种形式,其中一种是主要形式,名为MyForm,其他形式为{{1} },MyFormCaliMyFormAli等等。主要形式是使用MyFormPho方法调用其他方法。

问题是:

  1. 在我在show()类中创建的show_form()方法中,所有表单都在名为MyForm的字典类型变量中声明。当它发生时,实际上所有表单都已加载但没有显示。我可以看到调用了所有forms方法,并且没有可见的形式。我试着写一个很长的__init__语句,但后来我能够多次打开同一个表单。

  2. 当我关闭窗口时,它将无法正常关闭。我的意思是,在关闭和重新打开过程之后,我仍然可以看到表单发生了变化。例如,我正在向if添加一些值,重新打开后我仍然可以看到值。

  3. 长话短说,我需要一种更好的方法来打开/销毁另一个QListWidget

0 个答案:

没有答案