【发布时间】:2019-09-27 05:29:03
【问题描述】:
我有一个处理打开项目的父类。可以从调用父函数来处理打开项目的子窗口打开项目。但是,当从子窗口取消文件对话框时,整个应用程序都会退出。
from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
class ParentWindow(QDialog):
def __init__(self):
super(ParentWindow, self).__init__()
self.cw = None
self.setFixedSize(300, 100)
self.button = QPushButton('Open')
self.button.clicked.connect(self.open)
layout = QHBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.show()
def open(self):
fileDialog = QFileDialog(self, 'Projects')
fileDialog.setFileMode(QFileDialog.DirectoryOnly)
if fileDialog.exec():
self.hide()
name = fileDialog.selectedFiles()[0]
if self.cw:
self.cw.close()
self.cw = ChildWindow(self, name)
class ChildWindow(QDialog):
def __init__(self, parent, name):
super(ChildWindow, self).__init__(parent)
self.setFixedSize(500, 100)
self.setWindowTitle(name)
self.openButton = QPushButton('Open')
self.openButton.clicked.connect(self.parent().open)
layout = QHBoxLayout()
layout.addWidget(self.openButton)
self.setLayout(layout)
self.show()
我不明白为什么在文件拨号中按下取消时程序不会返回子窗口。有没有办法让父母负责打开项目并解决这个问题?
【问题讨论】:
-
我没听懂你的问题,你能解释一下吗
-
子窗口在按下按钮时创建一个 QFileDialog 实例。如果从 QFileDialog 中选择取消,则整个应用程序将退出。从 QFileDialog 按取消应该返回到上一个屏幕,即子窗口。
标签: python pyqt pyqt5 qdialog qfiledialog