我正在尝试移植我的 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>
我通过运行编译我的资源 QRC 文件:$ pipenv run pyside2-rcc -o src/ui/resources_rc.py src/ui/resources.qrc.
然后我通过运行编译我的 QML QRC 文件:pipenv run pyside2-rcc -o src/ui/qml_rc.py src/ui/qml.qrc
然后我使用以下命令运行我的程序:pipenv run python src/ui/main.py
UI 将弹出,但在控制台中有一些错误,特别是:
qrc:/qml/test.qml:25:9: QML Image: Cannot open: qrc:/resources/images/logo.svg