1. 程式人生 > >pyqt-webkit與javascript互動的問題

pyqt-webkit與javascript互動的問題

Qtwebkit中關於void QWebFrame::addToJavaScriptWindowObject的描述如下:

void QWebFrame::addToJavaScriptWindowObject ( const QString & name, QObject * object )

Make object available under name from within the frame's JavaScript context. The object will be inserted as a child of the frame's window object.

Qt properties will be exposed as JavaScript properties and slots as JavaScript methods.

If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal.

If Javascript is not enabled for this page, then this method does nothing.

The object will never be explicitly deleted by QtWebKit.

注意彩色文字部分,javascript呼叫的QObject的方法必須是槽才可以,而我用的pyqt,其中定義槽函式是有很多種方法,有什麼舊的方法新的方法,我採用的是使用  @pyqtSlot()

當槽有引數時必須在@pyqtSlot(str)新增引數,同樣當槽函式有返回值是也必須新增@pyqtSlot(result=int),否則在javascript程式碼中呼叫時都回失敗