73 lines
1.8 KiB
Python
73 lines
1.8 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
测试UI关闭时后台程序退出功能
|
||
|
|
"""
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
|
||
|
|
# 添加项目根目录到路径
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
def test_ui_exit():
|
||
|
|
"""测试UI关闭时的退出流程"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("测试 UI 关闭退出功能")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 模拟创建 QApplication
|
||
|
|
from PyQt5.QtWidgets import QApplication
|
||
|
|
app = QApplication(sys.argv)
|
||
|
|
|
||
|
|
# 导入 UI 模块
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'UI'))
|
||
|
|
from dtmgtUI import set_on_exit_callback
|
||
|
|
from views.main_window import MainWindow
|
||
|
|
|
||
|
|
# 设置退出回调
|
||
|
|
exit_callback_called = False
|
||
|
|
def on_exit():
|
||
|
|
nonlocal exit_callback_called
|
||
|
|
exit_callback_called = True
|
||
|
|
print("✓ 退出回调函数被调用")
|
||
|
|
app.quit()
|
||
|
|
|
||
|
|
set_on_exit_callback(on_exit)
|
||
|
|
|
||
|
|
# 创建主窗口
|
||
|
|
window = MainWindow()
|
||
|
|
|
||
|
|
# 连接关闭信号
|
||
|
|
def on_window_closed():
|
||
|
|
print("✓ 窗口关闭信号接收成功")
|
||
|
|
on_exit()
|
||
|
|
|
||
|
|
window.window_closed.connect(on_window_closed)
|
||
|
|
|
||
|
|
# 显示窗口
|
||
|
|
window.show()
|
||
|
|
|
||
|
|
print("\n提示: 请手动关闭窗口测试退出功能")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 运行应用
|
||
|
|
exit_code = app.exec_()
|
||
|
|
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("测试结果:")
|
||
|
|
print(f" 退出码: {exit_code}")
|
||
|
|
print(f" 回调调用: {'✓ 成功' if exit_callback_called else '✗ 失败'}")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
return exit_code
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
try:
|
||
|
|
exit_code = test_ui_exit()
|
||
|
|
sys.exit(exit_code)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"测试异常: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
sys.exit(1)
|