调用
QDialog.reject()
没有效果。我错过了什么?
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout
class MyDialog(QDialog):
def __init__(self):
super().__init__()
print('MyDialog created')
self.setWindowTitle('My Dialog')
button = QPushButton('Close')
button.clicked.connect(self.accept)
self.setLayout(QVBoxLayout())
self.layout().addWidget(button)
self.reject()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
button = QPushButton("Press Me!")
button.clicked.connect(self.show_dialog)
self.setCentralWidget(button)
def show_dialog(self):
d = MyDialog()
rv = d.exec()
if rv == QDialog.Accepted:
print('Dialog accepted')
else:
print('Dialog rejected')
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
运行程序时,会出现对话框,我可以按“关闭”。返回值为“已接受”。我希望对话框不会立即显示或关闭,并且返回值应该是“Rejected”
我错过了什么?