我正在尝试将我的C++ Qt应用程序移植到使用PySide2。在我的QML中,我有以下内容:
# test.qml
Image {
Layout.fillWidth: true
Layout.leftMargin: 5
Layout.rightMargin: 5
source: "qrc:/resources/images/logo.svg" # <-- Problematic line
fillMode: Image.PreserveAspectFit
asynchronous: true
}我的logo.svg文件在resources.qrc文件中被引用,如下所示:
<RCC>
<qresource prefix="/">
<file>resources/images/logo.svg</file>
</qresource>
</RCC>main.py看起来像:
import sys
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QUrl
import qml_rc
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load("qrc:/qml/main.qml")
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())qml.qrc看起来像:
<RCC>
<qresource prefix="/">
<file>qml/main.qml</file>
<file>qml/test.qml</file>
</qresource>
</RCC>我通过运行:$ pipenv run pyside2-rcc -o src/ui/resources_rc.py src/ui/resources.qrc编译我的资源QRC文件。
然后通过运行:pipenv run pyside2-rcc -o src/ui/qml_rc.py src/ui/qml.qrc编译QRC文件。
然后使用以下命令运行我的程序:pipenv run python src/ui/main.py
UI将弹出,但控制台中有一些错误,特别是:
qrc:/qml/test.qml:25:9: QML Image: Cannot open: qrc:/resources/images/logo.svg
发布于 2019-07-19 20:27:09
以导入qml_rc的方式,您还必须使用resources_rc:
import qml_rc
import resources_rc注意:用作组件的文件的名称必须如规则M16所指示的那样大写,我认为Test.qml只是用于示例的名称,在我的测试中使用了test.qml。
https://stackoverflow.com/questions/57118666
复制相似问题