Qt属性没有HTML中的通知信号更新将被破坏

时间:2017-02-24 05:54:50

标签: python qt pyqt pyqt5

在学习PyQT5的过程中,我遇到了这个例子

from PyQt5.Qt import *

qwebchannel_js = QFile(':/qtwebchannel/qwebchannel.js')
if not qwebchannel_js.open(QIODevice.ReadOnly):
    raise SystemExit(
        'Failed to load qwebchannel.js with error: %s' %
        qwebchannel_js.errorString())
qwebchannel_js = bytes(qwebchannel_js.readAll()).decode('utf-8')


def client_script():
    script = QWebEngineScript()
    script.setSourceCode(qwebchannel_js + '''
    new QWebChannel(qt.webChannelTransport, function(channel) {
        channel.objects.bridge.print('Hello world!');
    });
''')
    script.setName('xxx')
    script.setWorldId(QWebEngineScript.MainWorld)
    script.setInjectionPoint(QWebEngineScript.DocumentReady)
    script.setRunsOnSubFrames(True)
    return script


class WebPage(QWebEnginePage):
    def javaScriptConsoleMessage(self, level, msg, linenumber, source_id):
        try:
            print('%s:%s: %s' % (source_id, linenumber, msg))
        except OSError:
            pass

    @pyqtSlot(str)
    def print(self, text):
        print('From JS:', text)


app = QApplication([])
p = WebPage()
v = QWebEngineView()
v.setPage(p)
p.profile().scripts().insert(client_script())
c = QWebChannel(p)
p.setWebChannel(c)
c.registerObject('bridge', p)
v.setHtml('<p>Hello world!')
v.show()
app.exec_()

当我运行上面的代码时,我得到了控制台输出:

Property 'selectedText'' of object 'WebPage' has no notify signal and is not constant, value updates in HTML will be broken!

Property 'hasSelection'' of object 'WebPage' has no notify signal and is not constant, value updates in HTML will be broken!

Property 'requestedUrl'' of object 'WebPage' has no notify signal and is not constant, value updates in HTML will be broken!

Property 'title'' of object 'WebPage' has no notify signal and is not constant, value updates in HTML will be broken!

如何将属性定义为常量?

或者我如何附加通知信号? 我试过了:

self.titleChanged.connect(self.handleChange)

@pyqtSlot()
def handleChange(self):
    return

但控制台消息仍然存在。

0 个答案:

没有答案