Error: QQmlApplicationEngine failed to load component "Network problem"
Error: QQmlApplicationEngine failed to load component "Network problem"
我是 qml 的新手,python这是我得到的代码和错误:
错误:
QQmlApplicationEngine failed to load component
c:%5CUsers%5Cpcmcb%5COneDrive%5CDesktop%5Capp%5Capp%5Cqml/main.qml: Network error
main.py:
mport sys
import os
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
main.qml:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
ApplicationWindow{
id: window
width: 400
height: 580
visible: true
title: qsTr("Login Page")
}
当我尝试 运行 代码时,它给我上面输入的错误
load()
方法需要一个 QUrl,因此将字符串传递给它可能会导致问题,更改为:
import os
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication, Qt, QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
CURRENT_DIRECTORY = Path(__file__).resolve().parent
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
filename = os.fspath(CURRENT_DIRECTORY / "qml" / "main.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec())
if __name__ == "__main__":
main()
我是 qml 的新手,python这是我得到的代码和错误:
错误:
QQmlApplicationEngine failed to load component
c:%5CUsers%5Cpcmcb%5COneDrive%5CDesktop%5Capp%5Capp%5Cqml/main.qml: Network error
main.py:
mport sys
import os
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
main.qml:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
ApplicationWindow{
id: window
width: 400
height: 580
visible: true
title: qsTr("Login Page")
}
当我尝试 运行 代码时,它给我上面输入的错误
load()
方法需要一个 QUrl,因此将字符串传递给它可能会导致问题,更改为:
import os
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication, Qt, QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
CURRENT_DIRECTORY = Path(__file__).resolve().parent
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
filename = os.fspath(CURRENT_DIRECTORY / "qml" / "main.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec())
if __name__ == "__main__":
main()