0

I was learning Qt6, and I wrote a demo putting a local html file into it to test the QWebEngineView Widget. However, the web page shows the info:

Your file counldn't be accessed
It may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND

Here is my test.py source code:

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())

In Addition, the docs.html has been put into the same directory as the test.py file. And when I print the web.fileDir, the result is correct.

2 Answers2

0

In Qt in general is strongly preferred to use the qrc files, and the Qt resource management system. Here: Can QWebView load images from Qt resource files? is a small yet, neat example of something that is similar to your problem. You may also view the official PySide6 resource usage : https://doc.qt.io/qtforpython/tutorials/basictutorial/qrcfiles.html

Ilian Zapryanov
  • 1,132
  • 2
  • 16
  • 28
  • Since the latter answer works well, I havn't tried your answer yet. I am just a new beginner of Qt, I just know little about qrc for now. Maybe I will give it a try in the future. Thank you all the same! – EnkiduGilgamesh Nov 02 '21 at 11:08
0

You are hardcoded the url and the path may be wrong, in these cases the url is better step by step. I assume that the html is next to the .py then the solution is:

import os
from pathlib import Path
import sys

from PySide6.QtCore import QUrl
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView


CURRENT_DIRECTORY = Path(__file__).resolve().parent


class webView(QWidget):
    def __init__(self):
        super(webView, self).__init__()

        filename = os.fspath(CURRENT_DIRECTORY / "docs.html")
        url = QUrl.fromLocalFile(filename)

        self.webV = QWebEngineView()
        self.webV.load(url)

        layout = QVBoxLayout(self)
        layout.addWidget(self.webV)


if __name__ == "__main__":
    app = QApplication([])

    web = webView()
    web.show()

    sys.exit(app.exec())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks! It works perfect! And now I know what causes the issue. The true directory of "docs.html" is ".../project/test/docs.html" which is the same as this py file. But the absoluteFilePath() returns ".../project/docs.html". I thought the return may be related the working directory which actually is .../project. – EnkiduGilgamesh Nov 02 '21 at 11:01
  • Just change the "QFileInfo("./docs.html").absoluteFilePath()" into "QFileInfo("./test/docs.html").absoluteFilePath()" can fix the problem. But I am not sure if it can work correctly all the time. – EnkiduGilgamesh Nov 02 '21 at 11:16