我有一个带有mdi区域的主窗口,它将一个Qwidget添加为子窗口。
当我调用此QWidget并尝试通过单击关闭按钮来关闭它时,它只隐藏了QWidget,但是如果我点击QWidget的“X”按钮,它就会正确关闭。
我也需要通过关闭按钮来关闭QWidget。有人可以帮我这个吗?
非常感谢你!我的代码:
# -*- coding: utf-8 -*-
from PyQt4.QtCore import pyqtSignature
from PyQt4.QtGui import QMainWindow, QWidget
from PyQt4 import QtGui, QtCore
import sys
from ui.Ui_mainwindow import Ui_MainWindow
from ui.Ui_widget import Ui_widget
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) # does not make any difference, even if I comment this line
self.setWindowState(QtCore.Qt.WindowMaximized)
self.show()
@pyqtSignature("")
def on_action_widget_triggered(self):
w = Widget()
w.setAttribute(QtCore.Qt.WA_DeleteOnClose) # does not make any difference, even if I comment this line
self.mdiArea.addSubWindow(w)
w.showMaximized()
class Widget(QWidget, Ui_widget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
@pyqtSignature("")
def on_pbClose_clicked(self): # this is the "close" pushbutton. Whe i click it, the QWidget only hide. I need to close it, like if I click on It's "X" button
self.deleteLater()
self.close()
if __name__ == "__main__":
App = QtGui.QApplication(sys.argv)
frm = MainWindow()
sys.exit(App.exec_())
答案 0 :(得分:0)
使用QWorkspace解决,而不是QMdiArea
在mainwindow init上:
self.mdi = QtGui.QWorkspace()
self.setCentralWidget(self.mdi)
在QWidget的电话上:
w = Widget()
self.mdi.addWindow(w)
w.showMaximized()
所以,要关闭QWidget,只需:self.close()
source:“使用Python和Qt进行快速GUI编程 - PyQt编程权威指南(2008)”一书的第9章,第290页