You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#!/usr/bin/env python3
"""
Qt环境配置脚本, 确保平台插件正确加载
"""
import os
import sys
def setup_qt_environment ( ) :
""" 设置Qt环境变量, 避免平台插件问题 """
# 获取虚拟环境的site-packages路径
venv_path = os . environ . get ( ' VIRTUAL_ENV ' , ' ' )
if not venv_path :
print ( " 警告:未检测到虚拟环境 " )
return False
# 构建Qt插件路径
python_version = f " python { sys . version_info . major } . { sys . version_info . minor } "
qt_plugin_path = os . path . join ( venv_path , ' lib ' , python_version , ' site-packages ' , ' PyQt5 ' , ' Qt5 ' , ' plugins ' )
if not os . path . exists ( qt_plugin_path ) :
print ( f " 错误: Qt插件路径不存在: { qt_plugin_path } " )
return False
# 设置环境变量
os . environ [ ' QT_PLUGIN_PATH ' ] = qt_plugin_path
os . environ [ ' QT_QPA_PLATFORM_PLUGIN_PATH ' ] = os . path . join ( qt_plugin_path , ' platforms ' )
# 对于macOS, 还需要设置其他重要变量
if sys . platform == ' darwin ' :
os . environ [ ' QT_QPA_PLATFORM ' ] = ' cocoa '
# 禁用Qt的某些可能导致问题的功能
os . environ [ ' QT_MAC_WANTS_LAYER ' ] = ' 1 '
os . environ [ ' QT_AUTO_SCREEN_SCALE_FACTOR ' ] = ' 1 '
print ( f " ✅ Qt环境变量设置完成 " )
print ( f " QT_PLUGIN_PATH: { qt_plugin_path } " )
print ( f " QT_QPA_PLATFORM_PLUGIN_PATH: { os . environ [ ' QT_QPA_PLATFORM_PLUGIN_PATH ' ] } " )
return True
def verify_qt_setup ( ) :
""" 验证Qt设置是否正确 """
try :
from PyQt5 . QtCore import QCoreApplication
from PyQt5 . QtWidgets import QApplication
# 创建QApplication实例来测试
app = QCoreApplication . instance ( )
if app is None :
app = QApplication ( [ ] )
# 获取平台信息
platform = app . platformName ( )
print ( f " ✅ Qt平台检测成功: { platform } " )
return True
except Exception as e :
print ( f " ❌ Qt验证失败: { e } " )
return False
if __name__ == " __main__ " :
if setup_qt_environment ( ) :
verify_qt_setup ( )