EG/demo/test_size_fix.py
2025-07-02 09:49:59 +08:00

158 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试Qt环境下窗口尺寸获取修复
验证从Qt部件获取准确尺寸的功能
"""
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel
from PyQt5.QtCore import Qt
from QPanda3D.QPanda3DWidget import QPanda3DWidget
from QPanda3D.Panda3DWorld import Panda3DWorld
from panda3d.core import *
from direct.task import Task
class SizeTestWorld(Panda3DWorld):
def __init__(self):
super().__init__()
print("=== Qt窗口尺寸测试 ===")
self.setBackgroundColor(0.2, 0.2, 0.3)
# Qt部件引用
self.qtWidget = None
# 启动定时检查任务
self.taskMgr.doMethodLater(2.0, self.checkSizes, "check-sizes")
def setQtWidget(self, widget):
"""设置Qt部件引用"""
self.qtWidget = widget
print(f"✓ 设置Qt部件引用: {widget}")
print(f" Qt部件类型: {type(widget)}")
def getWindowSize(self):
"""获取准确的窗口尺寸"""
if self.qtWidget:
# 优先使用Qt部件的实际尺寸
width, height = self.qtWidget.getActualSize()
if width > 0 and height > 0:
print(f"✓ 从Qt部件获取窗口尺寸: {width} x {height}")
return width, height
# 备用方案使用Panda3D窗口尺寸
if hasattr(self, 'win') and self.win:
width = self.win.getXSize()
height = self.win.getYSize()
print(f"⚠ 从Panda3D窗口获取尺寸: {width} x {height}")
return width, height
# 最后的默认值
print("⚠ 使用默认窗口尺寸: 800 x 600")
return 800, 600
def checkSizes(self, task):
"""检查各种尺寸获取方法的结果"""
print("\n=== 窗口尺寸对比测试 ===")
# 方法1从Qt部件获取
if self.qtWidget:
qt_width, qt_height = self.qtWidget.getActualSize()
print(f"Qt部件尺寸: {qt_width} x {qt_height}")
else:
print("Qt部件尺寸: 未设置")
# 方法2从Panda3D窗口获取
if hasattr(self, 'win') and self.win:
panda_width = self.win.getXSize()
panda_height = self.win.getYSize()
print(f"Panda3D窗口尺寸: {panda_width} x {panda_height}")
else:
print("Panda3D窗口尺寸: 无效")
# 方法3使用新的getWindowSize方法
final_width, final_height = self.getWindowSize()
print(f"最终使用尺寸: {final_width} x {final_height}")
# 检查是否有差异
if self.qtWidget and hasattr(self, 'win') and self.win:
qt_width, qt_height = self.qtWidget.getActualSize()
panda_width = self.win.getXSize()
panda_height = self.win.getYSize()
if qt_width != panda_width or qt_height != panda_height:
print(f"⚠ 发现尺寸差异Qt: {qt_width}x{qt_height}, Panda3D: {panda_width}x{panda_height}")
print("这就是之前坐标轴点击检测失败的原因!")
else:
print("✓ 尺寸一致,没有问题")
# 每5秒检查一次
return task.again
class CustomSizeTestWidget(QPanda3DWidget):
"""支持尺寸获取的测试部件"""
def __init__(self, world, parent=None):
super().__init__(world, parent)
self.world = world
# 让world引用这个widget
if hasattr(world, 'setQtWidget'):
world.setQtWidget(self)
def getActualSize(self):
"""获取Qt部件的实际渲染尺寸"""
return (self.width(), self.height())
def resizeEvent(self, event):
"""处理窗口大小改变事件"""
super().resizeEvent(event)
print(f"\n窗口大小改变: {self.width()} x {self.height()}")
# 触发新的尺寸检查
if hasattr(self.world, 'checkSizes'):
self.world.taskMgr.doMethodLater(0.1, self.world.checkSizes, "check-sizes-after-resize")
def main():
print("启动Qt窗口尺寸测试...")
app = QApplication(sys.argv)
# 创建主窗口
mainWindow = QMainWindow()
mainWindow.setWindowTitle("Qt窗口尺寸测试 - 坐标轴修复验证")
mainWindow.setGeometry(100, 100, 900, 700) # 设置一个明确的大小
# 创建布局
centralWidget = QWidget()
layout = QVBoxLayout(centralWidget)
# 添加说明
label = QLabel("Qt窗口尺寸测试 - 查看控制台输出")
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet("background-color: lightgreen; padding: 10px; font-size: 14px;")
layout.addWidget(label)
# 创建世界
world = SizeTestWorld()
# 创建测试部件
pandaWidget = CustomSizeTestWidget(world)
layout.addWidget(pandaWidget)
mainWindow.setCentralWidget(centralWidget)
mainWindow.show()
print("\n测试说明:")
print("1. 程序会每5秒检查一次窗口尺寸")
print("2. 尝试调整窗口大小,观察尺寸变化")
print("3. 如果Qt尺寸和Panda3D尺寸不同说明修复有效")
print("4. 如果尺寸一致,说明你的环境没有这个问题")
return app.exec_()
if __name__ == "__main__":
sys.exit(main())