Error: QQmlApplicationEngine failed to load component quot;Network problemquot;(错误:QQmlApplicationEngine 无法加载组件“网络问题)
本文介绍了错误:QQmlApplicationEngine 无法加载组件“网络问题"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 qml 和 python 的新手这是我得到的代码和错误:
I'm new to qml and python This is the code and error I get:
错误:
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")
}
当我尝试运行代码时,它给了我上面输入的错误
When I try to run the code it gives me the error that I typed above
推荐答案
load()
方法需要一个 QUrl,因此传递一个字符串可能会导致问题,更改为:
load()
method requires a QUrl so passing it a string can cause problems, change to:
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()
这篇关于错误:QQmlApplicationEngine 无法加载组件“网络问题"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:错误:QQmlApplicationEngine 无法加载组件“网络问题"
基础教程推荐
猜你喜欢
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01