在QDialog中,按下ESC键可以触发QDialog的reject()方法,从而退出对话框。

以下是一个示例代码:

from PyQt5.QtWidgets import QDialog, QPushButton, QApplication

class MyDialog(QDialog):
    def __init__(self):
        super().__init__()

        button = QPushButton("Close", self)
        button.clicked.connect(self.close)

        self.setWindowTitle("My Dialog")

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.reject()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    dialog = MyDialog()
    dialog.show()
    sys.exit(app.exec_())

在这个示例中,当按下ESC键时,会触发keyPressEvent()方法,然后判断按下的键是否是ESC键,如果是,就调用reject()方法来退出对话框

QDialog esc怎么退出

原文地址: https://www.cveoy.top/t/topic/ipT9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录