问题描述
我正在尝试在 PyQt5 应用程序中使用 QML 中的 QtCharts。
Qt5 是通过 HomeBrew 安装的,并且似乎安装了 QtCharts 模块(除非通过 PyQt5 调用时它看起来在其他地方):
$ ls -1 /usr/local/opt/qt5/qml/QtCharts
designer
libqtchartsqml2.dylib
plugins.qmltypes
qmldir
在 PyQt5 中,我可以毫无问题地运行 QtQuick 应用程序,但是如果我尝试 import QtCharts 2.2
,我会收到一个错误:
未安装模块“QtCharts”
出现此问题的最小测试用例:
test_qml_qtcharts.py
import sys
from PyQt5.QtCore import QCoreApplication,QUrl
from PyQt5.QtQml import QQmlComponent,QQmlEngine
app = QCoreApplication(sys.argv)
engine = QQmlEngine()
component = QQmlComponent(engine)
component.setData(b'''
import QtQuick 2.9
import QtCharts 2.2
Item {}
''',QUrl('main.qml'))
instance = component.create()
if not instance:
for error in component.errors():
print(error.toString())
输出:
$ python3 test_qml_qtcharts.py
QQmlComponent: Component is not ready
main.qml:3:1: module "QtCharts" is not installed
删除 import QtCharts 2.2
测试运行良好。
解决方法
我认为它会调查系统的 Qt5 安装是错误的。
解决方案:通过pip安装PyQtChart:
python3 -m pip install PyQtChart