第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

QWebEnginePage 與 javascript 交互不起作用?

QWebEnginePage 與 javascript 交互不起作用?

森欄 2023-03-16 09:25:45
我不熟悉javascriptand QWebEnginePage。當(dāng)我設(shè)置self.content.setText('text')內(nèi)容時(shí),QWebEngineView內(nèi)容沒有改變?蟒蛇代碼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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

您有以下錯(cuò)誤:

  • channel 是一個(gè)局部變量,一旦“Demo”構(gòu)造函數(shù)完成就會(huì)被移除,它是 Python 和 Javascript 通信的中介。解決方案是通過將其傳遞給父類(Qt 樣式)或使其成為類的屬性來延長(zhǎng)生命周期。

  • 在 .html 中,您試圖包含 qwebchannel.js 但通常您應(yīng)該使用<script src="qrc:///qtwebchannel/qwebchannel.js"></script>.

  • 如果您將 QObject 導(dǎo)出到 javascript,那么只有 Q-Properties、QSlot 和 QSignals 將被導(dǎo)出,因?yàn)?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>


查看完整回答
反對(duì) 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)