为了账号安全,请及时绑定邮箱和手机立即绑定

QWebEnginePage 与 javascript 交互不起作用?

QWebEnginePage 与 javascript 交互不起作用?

森栏 2023-03-16 09:25:45
我不熟悉javascriptand QWebEnginePage。当我设置self.content.setText('text')内容时,QWebEngineView内容没有改变?蟒蛇代码class Document(QObject):    textChanged = pyqtSignal(str)    def __init__(self):        super().__init__()        self.text  = ''        def setText(self, text):        self.text = text        self.textChanged.emit(text)        print('emit')class Demo(QWebEngineView):    def __init__(self):        super().__init__()        self.content = Document()        page = self.page()        channel = QWebChannel()        channel.registerObject('content', self.content)        page.setWebChannel(channel)        with open('index.html') as f:            self.setHtml(f.read())        self.content.setText('text')app = QApplication([])demo = Demo()demo.resize(500, 400)demo.show()app.exec()索引html:<!doctype html><html><meta charset="utf-8"><head>    <script src="qwebchannel.js"></script></head><body><div id="placeholder">22</div><script>    'use strict';    var placeholder = document.getElementById('placeholder');    var updateText = function (text) {        placeholder.innerHTML = text;        console.log(text);    }    new QWebChannel(qt.webChannelTransport,        function (channel) {            var content = channel.objects.content;            updateText(content.text);            content.textChanged.connect(updateText);        }    );</script></body></html>
查看完整描述

1 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

您有以下错误:

  • channel 是一个局部变量,一旦“Demo”构造函数完成就会被移除,它是 Python 和 Javascript 通信的中介。解决方案是通过将其传递给父类(Qt 样式)或使其成为类的属性来延长生命周期。

  • 在 .html 中,您试图包含 qwebchannel.js 但通常您应该使用<script src="qrc:///qtwebchannel/qwebchannel.js"></script>.

  • 如果您将 QObject 导出到 javascript,那么只有 Q-Properties、QSlot 和 QSignals 将被导出,因为 QWebChannel 使用 QMetaObject instropection,但“文本”都不是它们,因此它在 javascript 中将是未定义的。解决方案是将其作为 pyqtProperty 公开。

import os

from PyQt5.QtCore import pyqtProperty, pyqtSignal, QObject, QUrl

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWebEngineWidgets import QWebEngineView

from PyQt5.QtWebChannel import QWebChannel



CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))



class Document(QObject):

    textChanged = pyqtSignal(str)


    def __init__(self):

        super().__init__()

        self._text = ""


    def text(self):

        return self._text


    def setText(self, text):

        self._text = text

        self.textChanged.emit(text)

        print("emit")


    text = pyqtProperty(str, fget=text, fset=setText, notify=textChanged)



class Demo(QWebEngineView):

    def __init__(self):

        super().__init__()


        self.content = Document()


        channel = QWebChannel(self)

        channel.registerObject("content", self.content)

        self.page().setWebChannel(channel)


        filename = os.path.join(CURRENT_DIR, "index.html")

        self.load(QUrl.fromLocalFile(filename))


        self.content.setText("text")



def main():

    app = QApplication([])

    demo = Demo()

    demo.resize(500, 400)

    demo.show()

    app.exec()



if __name__ == "__main__":

    main()

<!doctype html>

<html>

<meta charset="utf-8">

<head>

    <script src="qrc:///qtwebchannel/qwebchannel.js"></script>

</head>

<body>

<div id="placeholder">22</div>

<script>

    'use strict';


    var placeholder = document.getElementById('placeholder');


    var updateText = function (text) {

        placeholder.innerHTML = text;

        console.log(text);

    }


    new QWebChannel(qt.webChannelTransport,

        function (channel) {

            var content = channel.objects.content;

            updateText(content.text);

            content.textChanged.connect(updateText);

        }

    );

</script>

</body>

</html>


查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号