gpt4 book ai didi

python - 使用 QtQuick.Controls 中的任何内容都会引发 protected 模块错误

转载 作者:行者123 更新时间:2023-12-04 09:37:32 27 4
gpt4 key购买 nike

当我尝试启动程序时出现这些错误:

plugin cannot be loaded for module, cannot install type into protected module



平台:
python 3.8
PyQt5 5.15.0
Visual Studio 社区 2019
window 10 专业版 1909
主 python 文件 (整个事情几乎就是这里的例子: https://codeloop.org/pyqt5-creating-first-window/)
import numpy
import os
import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication

def main():
app =QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "MainApp.qml"))


if not engine.rootObjects():
return -1

return app.exec_()

if __name__ == '__main__':
main();
对应的 QML 文件“ MainApp.qml ”:
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick 2.15

Window {

visible:true
width:600
height:400
color:"yellow"
title: "PyQt5 QML Window"

Button {
text: "Something"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}


}
不使用 QtQuick.Controls 中的任何东西,它就可以工作,但是一旦我添加按钮,它就会生成以下错误:
QQmlApplicationEngine failed to load component
file:///C:/Users/elano/Source/Repos/Projekt-1-SS2020/Projekt-1-SS2020/MainApp.qml:15:5: Type Button unavailable
file:///C:/Users/elano/vpqt/lib/site-packages/PyQt5/Qt/qml/QtQuick/Controls.2/qmldir: plugin cannot be loaded for module "QtQuick.Controls": Cannot install type 'VerticalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install type 'HorizontalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install element 'SplitHandle' into protected module 'QtQuick.Controls' version '2'
...
它尝试查找的文件 (qmldir) 确实存在,并且包含以下内容:
module QtQuick.Controls
plugin qtquickcontrols2plugin
classname QtQuickControls2Plugin
depends QtQuick.Templates 2.5
designersupported
有谁知道这里发生了什么?是否需要更多信息?

最佳答案

好吧,现在我觉得自己很愚蠢。我已经在我的代码中有我的答案,但由于它没有让 Pyside2 工作,我把它注释掉了......
问题是,环境变量设置不正确。在我的主文件中的导入语句之后添加它修复了它:

dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QML2_IMPORT_PATH'] = os.path.join(dirname, 'qml')
我发现没有任何教程提到过这一点。伟大的。

关于python - 使用 QtQuick.Controls 中的任何内容都会引发 protected 模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62500044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com