python - 为什么我的本地 html 加载到 PySide6 QWebEngineView 时无法访问?
问题描述
我正在学习 Qt6,我写了一个演示,将本地 html 文件放入其中以测试 QWebEngineView Widget。但是,网页显示信息:
Your file counldn't be accessed
It may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND
这是我的test.py
源代码:
import sys
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout)
from PySide6 import QtCore
from PySide6.QtWebEngineWidgets import QWebEngineView
class webView(QWidget):
def __init__(self):
super(webView, self).__init__()
self.layout = QVBoxLayout(self)
self.webV = QWebEngineView()
self.fileDir = QtCore.QFileInfo("./docs.html").absoluteFilePath()
print(self.fileDir)
self.webV.load(QtCore.QUrl("file:///" + self.fileDir))
self.layout.addWidget(self.webV)
if __name__ == "__main__":
app = QApplication([])
web = webView()
web.show()
sys.exit(app.exec())
此外,docs.html
已将文件放入与test.py
文件相同的目录中。当我打印时web.fileDir
,结果是正确的。
解决方案
一般来说Qt
,强烈首选使用qrc
文件和Qt
资源管理系统。这里:QWebView 可以从 Qt 资源文件中加载图像吗? 是与您的问题类似的一个小而简洁的示例。您还可以查看官方PySide6
资源使用情况:
https ://doc.qt.io/qtforpython/tutorials/basictutorial/qrcfiles.html
推荐阅读
- java - 如何发送输入而不让用户在 Android Studio 中看到它
- python - 为什么即使有对类型的引用,第二个片段的输出也不是 40?
- python - 使用python循环分页API
- java - 了解 Java PriorityQueue 中的比较器
- javascript - 在设置选项内的 axios 响应回调中更新反应数据 - Vue 3
- php - PHP - 附加到对象?
- python - Numpy 将 3D 数组映射到 2D 数组,但数组不匹配
- sql - 获取除最后 5 行之外的所有行
- javascript - 本地存储操作
- java - Spring Boot 不会将 @Autowired 存储库注入 netty 类