空 QMainWindow,PyQt

Posted

技术标签:

【中文标题】空 QMainWindow,PyQt【英文标题】:Empty QMainWindow, PyQt 【发布时间】:2014-05-17 08:32:04 【问题描述】:

我有两个 QMainWindows,我正试图从另一个调用一个。窗口打开,但是,它是空的,直到父窗口关闭才响应。

我的代码:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(184, 165)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 184, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()"),self.gone)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

    def gone(self):
        mainwindow = QtGui.QMainWindow()
        self.mw2 = Ui_MainWindow1()
        self.mw2.setupUi(mainwindow)
        self.mw2.show()

class Ui_MainWindow1(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(170, 175)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(40, 60, 101, 16))
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 170, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "This is second", None, QtGui.QApplication.UnicodeUTF8))

def startmain():
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()

if __name__ == "__main__":
    import sys
    startmain()

【问题讨论】:

【参考方案1】:

当您的 Ui_MainWindowUi_MainWindow1 类是 QMainWindow 的子类时,我不明白您为什么要创建 QMainWindow 对象。基本上,您有四个主窗口,而您只需要两个。

不要创建称为MainWindowQMainWindows 并在其中进行设置,而是使用self 而不是MainWindow 来设置每个窗口本身。以下似乎对我有用,因为第一个窗口有一个按钮,单击它会打开带有标签的第二个窗口:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)

    def setupUi(self):
        self.setObjectName(_fromUtf8("self"))
        self.resize(184, 165)
        self.centralwidget = QtGui.QWidget(self)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 184, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(self)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        self.setStatusBar(self.statusbar)

        self.retranslateUi()
        QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()"),self.gone)
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

    def gone(self):
        self.mw2 = Ui_MainWindow1()
        self.mw2.setupUi()
        self.mw2.show()

class Ui_MainWindow1(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)

    def setupUi(self):
        self.setObjectName(_fromUtf8("self"))
        self.resize(170, 175)
        self.centralwidget = QtGui.QWidget(self)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(40, 60, 101, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 170, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(self)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        self.setStatusBar(self.statusbar)

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

    def retranslateUi(self):
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "This is second", None, QtGui.QApplication.UnicodeUTF8))

def startmain():
    app = QtGui.QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.setupUi()
    ui.show()
    app.exec_()

if __name__ == "__main__":
    import sys
    startmain()

【讨论】:

谢谢卢克,我只是想让子窗口进入父窗口,并且在父窗口关闭之前处于非活动状态

以上是关于空 QMainWindow,PyQt的主要内容,如果未能解决你的问题,请参考以下文章

从另一个 QMainWindow 调用时,QMainWindow 消失

PySide:为啥在另一个 QmainWINdow(Main) 中触发 QAction 时 QMainWindow 会烧毁并消失

如何从父级不是 QMainWindow 的 QWidget 访问 QMainWindow

QT:在其他窗口中显示QMainWindow

在 Pyqt4 中用不同的 QMainWindow 刷新 QMainWindow

如何将 QMainWindow 调整大小事件传递给 QMainWindow 中包含的 QGLWidget?