Gau*_*wal 5 qt facebook qml facebook-graph-api facebook-javascript-sdk
我正在尝试让QML使用FB Javascript SDK与FB图形API进行交互.
我在WebView元素中加载此HTML:
html: "<script>console.log(\"This is in WebKit!\"); window.FB.init();</script>"
Run Code Online (Sandbox Code Playgroud)
我还在WebView中创建了一个名为FB的JS窗口对象:
javaScriptWindowObjects: QtObject {
WebView.windowObjectName: "FB"
}
Run Code Online (Sandbox Code Playgroud)
但是只要调用window.FB.init(),它就会抛出一个错误:
ReferenceError: Can't find variable: window
Run Code Online (Sandbox Code Playgroud)
我正在使用的另一种方法是使用Component.onComplete加载FB.init()函数
function startupFunction() {
console.log("This call is in QML!");
FB.init({
appId:'XXXXXXXXXXXXX', cookie:true,
status:true
});
console.log(FB);
}
Component.onCompleted: startupFunction();
Run Code Online (Sandbox Code Playgroud)
但我得到的错误是:
TypeError: Result of expression 'FB.init' [undefined] is not a function
Run Code Online (Sandbox Code Playgroud)
这是完整的QML:
import QtQuick 1.0
import "fb.js" as FB
import QtWebKit 1.0
Rectangle {
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
}
WebView {
preferredWidth: 490
preferredHeight: 400
scale: 0.5
smooth: false
javaScriptWindowObjects: QtObject {
WebView.windowObjectName: "FB"
}
html: "<script>console.log(\"This is in WebKit!\"); window.FB.init();</script>"
function startupFunction() {
console.log("This call is in QML!");
FB.init({
appId:'xxxxxxxxxxxx', cookie:true,
status:true
});
console.log(FB);
}
Component.onCompleted: startupFunction();
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于您没有在窗口对象中定义任何内容,您QtObject只包含windowObjectName但不包含函数或变量。windowObjectName实际上只是新对象的名称,qml 不"fb.js"对该对象使用 -import 。
根据文档,它应该看起来像这样:
WebView {
javaScriptWindowObjects: QtObject {
WebView.windowObjectName: "FB"
// the stuff you want in that window-object goes here:
function init() {
console.log("FB.init");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |