我正在尝试将 PySide 应用程序版本 #1 移植到 PySide2 并努力寻找移植以下代码片段的解决方案:

class AppManager(QtCore.QObject):
    '''
    methods of AppObject will be available from javascript
    '''
    def __init__(self, webview):
        QtCore.QObject.__init__(self)


class WebView(QWebEngineView):
    def __init__(self, parent=None):
        QWebEngineView.__init__(self, parent)
        self.setPage(WebEnginePage(self))

    def contextMenuEvent(self, event):
        pass


class AppWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.view = WebView(self)
        self.page = self.view.page()
        self.app_manager = AppManager(self.view)
        self.page.mainFrame().addToJavaScriptWindowObject('app_manager', self.app_manager)
        # ERROR in above line !!!

我在文档中找不到我必须找到可能的修复方法

最佳答案

您必须使用 QWebChannel,为此您必须下载 qwebchannel.js(链接指向 qwebchannel.js for Qt 5.12 ,不同版本更改 github 分支)。

在下面的代码中,我展示了一个例子:

.
├── index.html
├── main.py
└── qwebchannel.js

main.py
import sys

from PySide2 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel


class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    pass

class AppManager(QtCore.QObject):
    textChanged = QtCore.Signal(str)
    def __init__(self, webview):
        QtCore.QObject.__init__(self)
        self.m_text = ""

        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.on_timeout)
        timer.start(1000)

    def on_timeout(self):
        self.text  = QtCore.QDateTime.currentDateTime().toString()

    @QtCore.Property(str, notify=textChanged)
    def text(self):
        return self.m_text

    @text.setter
    def setText(self, text):
        if self.m_text == text:
            return
        self.m_text = text
        self.textChanged.emit(self.m_text)


class WebView(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, parent=None):
        QtWebEngineWidgets.QWebEngineView.__init__(self, parent)
        self.setPage(WebEnginePage(self))

    def contextMenuEvent(self, event):
        pass


class AppWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.view = WebView(self)
        self.page = self.view.page()
        self.app_manager = AppManager(self.view)
        channel = QtWebChannel.QWebChannel(self)
        self.page.setWebChannel(channel)
        channel.registerObject("app_manager", self.app_manager)
        self.view.load(QtCore.QUrl.fromLocalFile(QtCore.QDir.current().filePath("index.html")))
        self.setCentralWidget(self.view)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())

index.html
<html>
<head>
    <script type="text/javascript" src="./qwebchannel.js"></script>
</head>
<header><title>This is title</title></header>
<body>

<p id="output"></p>

<script type="text/javascript">
    window.onload = function() {
        new QWebChannel(qt.webChannelTransport, function (channel) {
            window.app_manager = channel.objects.app_manager;
            console.log(app_manager);

            app_manager.textChanged.connect(function(message) {
                document.getElementById("output").innerHTML = "Received message: " + message;
            });

        });
    }
</script>
</body>
</html>

关于python - PySide2 并支持 addToJavaScriptWindowObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51011244/

10-13 08:11