pyQt5 将文件对象传递给 QWebEngineView

pyQt5 pass a file object to QWebEngineView

本文关键字:QWebEngineView 对象 文件 pyQt5      更新时间:2023-10-16

如何将主应用程序中打开的文件发送到QWebEngineView中的页面并通过javascript FileReader((函数处理它,因为它是由QWebEngineView中的html5文件输入打开

这是我代码的一部分

# -*- coding: utf-8 -*-
import sys,  os
from PyQt5.QtWidgets import ( QApplication,  QMainWindow)
from PyQt5.QtCore import QUrl
import PyQt5.QtWebEngineWidgets as QtWebEngineWidgets
import interface
class MyWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.ui = interface.Ui_MainWindow()
        self.ui.setupUi(self)
        web_view = QtWebEngineWidgets.QWebEngineView()
        self.web_view = web_view
        self.ui.verticalLayout_navigateur.addWidget(web_view)
        url = self.local_url("src/index.html")
        self.web_view.load(url)
        f = open('myfile.json', 'r') 
        #send f to self.web_view and handle it by javascript FileReader() function
    def local_url(self, relativePath):
        absolutePath = os.path.abspath(relativePath)
        url = QUrl.fromLocalFile(absolutePath)
        return url
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

谢谢

我找到了另一种解决方案,它非常简单,而不是发送对象文件来读取 JavaScript,我只需要在主应用程序中读取它并通过 QWebChannel 注入内容