假设我有2个窗口,其中一个在菜单项上打开另一个窗口:
class ProjectWindow(QtWidgets.QMainWindow, project_window_qt.Ui_ProjectWindow):
def __init__(self):
super(ProjectWindow, self).__init__()
# Setup the main window UI
self.setupUi(self)
self.new_project_window = None
# Handle menu bar item click events
self.actionNewProject.triggered.connect(self.new_project)
def new_project(self):
self.new_project_window = project_new_window.NewProjectWindow()
self.new_project_window.show()
def refresh_projects(self):
with open(os.path.join(self.directory, 'projects.txt'), 'r') as f:
projects = json.load(f)
return projects
和
class NewProjectWindow(QtWidgets.QDialog, project_new_window_qt.Ui_NewProjectWindow):
def __init__(self,):
super(NewProjectWindow, self).__init__()
# Setup the main window UI
self.setupUi(self)
用户关闭new_project_window
后,我希望在refresh_projects
类中调用ProjectWindow
方法。
我考虑过设置一个事件监听器来检查new_project_window
何时关闭,然后在发生这种情况后调用refresh_projects
,但窗口在打开后立即关闭:
def new_project(self):
self.new_project_window = project_new_window.NewProjectWindow(self.directory, self.project_list)
self.new_project_window.onClose.connect(self.refresh_projects)
self.new_project_window.show()
这是正确的方法吗?或者有没有办法直接从refresh_projects
对象中调用new_project_window
?
答案 0 :(得分:0)
如果您使用的是QDialog
,则应调用exec_()
而不是show(),这将在用户关闭窗口时返回一个值,然后只调用刷新项目。
def new_project(self):
self.new_project_window = project_new_window.NewProjectWindow(self.directory, self.project_list)
code = self.new_project_window.exec_()
"""code: gets the value of the QDialog.Accepted, or QDialog.Rejected
that you can connect it to some accept button
using the accept() and reject() functions.
"""
self.refresh_projects()
exec_()
正在屏蔽,即除非QDialog
已关闭,否则不会执行下一行。