PyQt4 |处理QDialog中的“X”退出按钮

2024-05-18 12:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

几个星期以来,我一直在编写一个插件到一些著名的GIS免费软件(QGIS)。我有点小问题。在

我的插件方案如下:

.......
class DisplayedWindow(object):

        def __init__(self):
            #JANEK Main dialog
            self.window_plugin = QtGui.QDialog()
            self.window_plugin.setWindowModality(QtCore.Qt.WindowModal)
            self.window_plugin.setGeometry(150, 150, 750, 675)
            self.window_plugin.setWindowTitle('the plugin')
            self.window_plugin.setWindowFlags(Qt.WindowMinimizeButtonHint|Qt.WindowMaximizeButtonHint) 

            ............. (GUI, functions, etc.).......

def run(self):

    dis_win = self.DisplayedWindow()
    if dis_win.window_plugin.exec_():
        pass

我知道它的建筑不是它应该的,但我是乞丐。好的是这个插件工作得非常好,我对它的编码太过深入,以至于现在无法改变整个程序的结构。在

我要找的是一种处理X-exit红色按钮的方法,这样用户可以在关闭窗口之前被询问是否不想保存更改等

我需要self.X_close_button.clicked.connect(lambda: closing_stuff())

有人知道怎么收到吗?或者在这样一个对话框中的任何其他方式来控制有人关闭窗口后发生的事情(self.window_插件)? 在

祝大家今天愉快!在


Tags: self插件objectdef方案免费软件windowqt
2条回答

扩展QDialog并重写其closeEvent()方法:

class GISDialog(QDialog):
    def __init__(self, parent=None):
        super(GISDialog, self).__init__(parent)

        self.setGeometry(150,150,750,750)
        self.window_plugin.setWindowTitle('the plugin')
        # other intitialization

    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
            "Do you want to save?", QMessageBox.Yes, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

然后,当您准备好使用它时:

^{pr2}$

我给你举了个简单的例子。整个代码如下:

import sys
from PyQt4 import QtGui,QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Create_Dialog_Box(QDialog):
    def __init__(self,parent = None):
        super(Create_Dialog_Box, self).__init__(parent)
        self.setGeometry(100,100,500,200)
    def closeEvent(self,event):
        quit_msg = "Are you sure you want to exit the dialog?"
        reply = QtGui.QMessageBox.question(self, 'Message', 
                        quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.setGeometry(100,100,200,50)

d = Create_Dialog_Box(w)

b = QtGui.QPushButton(w)
b.setText("Click Me!")
b.move(50,20)
b.clicked.connect(d.show)


w.setWindowTitle("PyQt")
w.show()
print("End")
sys.exit(app.exec_())

当您尝试退出该对话框时,会出现如下所示的提示:

enter image description here

希望有帮助。在

相关问题 更多 >

    热门问题