基于QtQuick的C++,qml,Html三者的Bridge交互
主要功能
自定義C++對象并注冊到QML
可以在QML中繼續擴展C++對象的屬性和方法 信號等
將C++對象注入到WebChannel
Html中引入C++對象
JavaScript與C++直接交互
示例代碼說明
main.cpp:
//該實例必須直接繼承自Object的 QWidget的忽略
qmlRegisterType("DDuiObejct",1,0,"DDQmlObejct");
qml:
//需要將這個對象注冊到channel里面
DDQmlObejct{
id: myObject
// the identifier under which this object
// will be known on the JavaScript side
// signals, methods and properties are
// accessible to JavaScript code
WebChannel.id: "DDCore" //這個id可在html中使用
//qml中繼續擴展信號
signal someSignal(string message);
//監聽C++的信號
onSignalSendToQml: {
messageId.append(message);
}
//自定義函數
function someMethod(message) {
console.log(message);
someSignal(message);
slotShowMessage(message)
return "someMethod:"+message;
}
//擴展的函數 可以定義很多函數
function getThisFilePath(filepath){
var d = getFileContents(filepath);
someMethod(d);
return d
}
//擴展屬性
property string hello: "world"
}
===========以下為webengineview的實例和WebChannel的實例===============
//需要注冊一個WebChannel對象
WebChannel{
id:changedId
registeredObjects:[myObject]//可以將多個對象注冊到這里
}
//主web界面
WebEngineView {
id:webview
anchors.left: parent.left
anchors.top: parent.top
width: parent.width/2
anchors.bottom: parent.bottom
url: "qrc:/chatRecord.html"
webChannel:changedId//指定該對象的溝通橋梁WebChannel
}
有問題反饋
在使用中有任何問題,歡迎反饋給我,可以用以下聯系方式跟我交流
QQ: 373955953
QQ群:312125701
github: @寒山-居士
關于作者
var duoduozhijiao = {
nickName : "寒山-居士",
site : "http://www.heilqt.com",
blog : "http://blog.heilqt.com"
}