我试图在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