當前位置:首頁 > 芯聞號 > 充電吧
[導讀]流程有些復雜,QML不支持調(diào)用很多常見的js引擎,我們可以利用webview來達到。不過在使用socket.io,發(fā)現(xiàn)必須要在安卓4.4版本或更高才行,這個不是安卓的問題,是最新版的Qt沒有優(yōu)化老版本

流程有些復雜,QML不支持調(diào)用很多常見的js引擎,我們可以利用webview來達到。不過在使用socket.io,發(fā)現(xiàn)必須要在安卓4.4版本或更高才行,這個不是安卓的問題,是最新版的Qt沒有優(yōu)化老版本的WebView,唉。
開發(fā)流程圖:
從發(fā)送到回調(diào):?QML?->?WEBVIEW中的socke.io??->?node.js服務器??->WEBVIEW??->?(標注)C++?->?QML
標注:由于socket.io是異步的,QML調(diào)用JS允許有回調(diào),但是不支持在WEBVIEW中的異步回調(diào)。因此用C++來轉發(fā)。
#ifndef?TQMLHELPER_H
#define?TQMLHELPER_H

#include#include#includeclass?TQmlHelper?:?public?QObject
{
????Q_OBJECT
public:

????explicit?TQmlHelper(QObject?*parent?=?0);
//????代碼規(guī)范:??set開頭???供QML調(diào)用??用來設置本類的QML對象
//???????????????do開頭????給HTTP服務器調(diào)用??用來間接通信QML
????Q_INVOKABLE?void??setroot(QObject*?obj);
????Q_INVOKABLE?void??setTXL(QObject*?obj);

????//從聊天服務器獲取好友?請求Qml刷新好友列表
????void?do_updataFlist(QByteArray&?b);

signals:

public?slots:

?private:
QObject*??root;

//PageTongxunlv.qml:
//Component.onCompleted:?{
//????????myapp.setTXL(gen);
//?????????console.log("cout:"+model.count);
//?????????model.append({name:"代碼統(tǒng)計行數(shù):"+model.count,tip:"代碼生成的行"});
//????}
//PageTongxunlv中的跟節(jié)點?包含好友列表的一些成員函數(shù)
QObject*??gen;
};

#endif?//?TQMLHELPER_H
#include?"tqmlhelper.h"
#include#includeTQmlHelper::TQmlHelper(QObject?*parent)?:?QObject(parent)
{
????????root?=?NULL;
}



void??TQmlHelper::setroot(QObject*?obj)
{
????qDebug()<>>:"<<?obj->objectName();
}

?void?TQmlHelper::do_updataFlist(QByteArray&?b)
?{
????????????qDebug()<<"do_updataFlist:"<<b;
????????????QString?a?=?b;?;
????????????QMetaObject::invokeMethod(gen,?"刷新好友列表",?Qt::ConnectionType::BlockingQueuedConnection,?Q_ARG(QVariant,?a));

?}
#include"myhttp.h"
#includeextern?TQmlHelper*?tmphelp;
Helloworldcontroller3::Helloworldcontroller3(QObject*?parent):HttpRequestHandler(parent)
{

}
#includevoid?Helloworldcontroller3::service(HttpRequest?&request,?HttpResponse?&response)?{

????//允許跨域
??????response.setHeader("Access-Control-Allow-Origin",?"*");?

?????????QByteArray?path=request.getPath();
????????qDebug("RequestMapper:?path=%s",path.data());

????????//登錄狀態(tài)
????????if?(path=="/loging")?{
????????????QByteArray?a?=?request.getParameter("p1");
????????????qDebug()<<a;

????????????if(a.toInt()==1)
????????????{
????????????????qDebug()<<"loging?ok";
????????????}else{
????????????????qDebug()<<"loging?erro";
????????????}
?????????????response.setStatus(200,"OK");
???????????//?HelloWorldController().service(request,?response);
????????}
?????????//獲取好友列表的返回結果
????????else?if?(path=="/Flist")?{
?????????????QByteArray?a?=?request.getParameter("p1");
?????????????tmphelp->do_updataFlist(a);
?????????????response.write("ok",true);
?????????????response.setStatus(200,"ok");
????????}

????????else?{
????????????response.setStatus(404,"oo!Not?found");
????????????QString??s?=QString::fromLocal8Bit("請請不要跨域哦");
????????????QByteArray?s2(s.toStdString().c_str());
????????????response.write(s2,true);
????????}

????????qDebug("RequestMapper:?finished?request");
}
*****main.qml
TQmlHelper*??tmphelp;
void??RegFunc(QQmlApplicationEngine&??engine)
{

???????tmphelp?=?new?TQmlHelper(&engine);

????????engine.rootContext()->setContextProperty("myapp",?(QObject*)tmphelp);
}
#includeint?main(int?argc,?char?*argv[])
{


????QApplication::setApplicationName("Myapp");
???QApplication::setOrganizationName("QtProject");
?????QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);

//?????qputenv("QT_SCALE_FACTOR",?b);
//?????system("su");
????QApplication?app(argc,?argv);

//????MyWait*??label?=?new?MyWait();
//????label->show();

???//

qDebug("********開始*********");
????QSettings*?listenerSettings=?new?QSettings("assets:/demo.ini",QSettings::IniFormat,&app);

????qDebug("config?file?loaded");

????listenerSettings->beginGroup("listener");

???????//?Start?the?HTTP?server

???????new?HttpListener(listenerSettings,?new?Helloworldcontroller3(&app),?&app);

?????QQuickStyle::setStyle("Material");
????QQmlApplicationEngine?engine;


???RegFunc(engine);
????engine.load(QUrl(QLatin1String("qrc:/main.qml")));
????if?(engine.rootObjects().isEmpty())
????????????return?-1;

//????label->hide();

qDebug("********結束*********");
????return?app.exec();
}
由于如果用C++主動獲取QML對象,可能存在QML對象尚未初始化問題,因此,在qml初始化后調(diào)用C++來設置QML對象指針。
import?QtQuick?2.7
import?QtQuick.Controls?2.0
import?QtQuick.Dialogs?1.2
import?QtQuick.Layouts?1.1
import?QtQuick.Window?2.0
import?QtQuick.Controls.Styles?1.4
import?QtQuick.Controls.Material?2.0
import?QtGraphicalEffects?1.0
import?QtQuick.Particles?2.0
import?"Ndk.js"???as??Ndk
import?"./code"
import?QtWebView?1.1

?ApplicationWindow?{
????id:root;
????visible:?true;



?????height:?480;
?????width:?320;
????Component.onCompleted:?{

????????//把窗口對象傳給Qt?之所以加載完畢后才傳遞?很多對象提前是無內(nèi)存塊
????????myapp.setroot(root);

????}

????property?real?pixelDensity:?4.46
????property?real?multiplierH:?root.height/480?//default?multiplier,?but?can?be?changed?by?user
????property?real?multiplierW:?root.width/320?//default?multiplier,?but?can?be?changed?by?user
????function?dpH(numbers)?{

????????return?Math.round(numbers*((pixelDensity*25.4)/160)*multiplierH);
????}
????function?dpW(numbers)?{
????????return?Math.round(numbers*((pixelDensity*25.4)/160)*multiplierW);
????}
????function??dpi2(px)
????{

????????return?Math.round(px*((Screen.pixelDensity*25.4)/160))
????}


????/**
????????*?將px值轉換為dip或dp值,保證尺寸大小不變
????????*
????????*?@param?pxValue
????????*?@param?scale
????????*????????????(DisplayMetrics類中屬性density)
????????*?@return
????????*/
????function???px2dip(pxValue)?{
????????var?scale?=?myapp.getdensity();
????????return??(pxValue?/?scale?+?0.5);
????}

????property?color??accentcol:"red"
????property?color??backgroundcol:"white"
????property?color??foregroundcol:"#000000"
????property?color??primarycol:"blue"
????Material.accent:accentcol
????Material.background:backgroundcol
????Material.foreground:foregroundcol
????Material.primary:?primarycol

//消息框
????????????MessageDialog?{
????????????????id:dlg;
????????????????objectName:?"xiaoxikuang";
????????????????width:?Screen.width;
????????????????height:?Screen.hight;
????????????????visible:?false;
??????????????????title:?"進度"
????????????????????icon:?StandardIcon.Question
????????????????????text:?"file.txt?already?exists.??Replace?"
????????????????????detailedText:?"To?replace?a?file?means?that?its?existing?contents?will?be?lost.?"?+
????????????????????????"The?file?that?you?are?copying?now?will?be?copied?over?it?instead."
????????????????????standardButtons:?StandardButton.Yes?|?StandardButton.No?
????????????}
????????????function?messaggeBox(title,txt)
????????????{
????????????????dlg.text?=?txt;
????????????????dlg.title?=?title;
????????????????dlg.visible?=?true;
????????????}


//調(diào)試打印

????????????Item?{
????????????????visible:?true
????????????????width:parent.width;
????????????????height:dpH(100);
????????????????z:100

//????????????????????color:Ndk.SDColor_warning;
????????????????????????ListView{
????????????????????????????id:dbgtxt;
????????????????????????????width:parent.width;
????????????????????????????height:dpH(100);
????????????????????????????model:?ListModel{
????????????????????????????????id:dbgmodel;
????????????????????????????????ListElement{txt:"123"}
????????????????????????????}
????????????????????????????delegate:?Label{
????????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????????font.bold:?true;
????????????????????????????????color:?Ndk.green_1
????????????????????????????????text:txt
????????????????????????????}
????????????????????????????displaced:?Transition?{
??????????????????????????????????????NumberAnimation?{?properties:?"x,y";?duration:?1000?}
??????????????????????????????????}
????????????????????????????move:?Transition?{
??????????????????????????????????????NumberAnimation?{?properties:?"x,y";?duration:?1000?}
??????????????????????????????????}
????????????????????????}
????????????????}
????????????function?dbg(txt)
????????????{
????????????????if(dbgmodel.count>=5)
????????????????{
????????????????????dbgmodel.clear();
????????????????}
????????????????dbgmodel.append({txt:txt});
????????????}


????//////////////////////加載控制器//////////////////////////
????property?alias?mwebview:?mywebview;


????????????property?var?txtprogress_var:?0
????????????property?var?txtmainpage_var:?0
?????????????property?var?txtsubpage1_var:?0
????????????Item?{
????????????????width:?Screen.width;
????????????????height:?Screen.height
????????????????visible:?true
????????????????z:100
????????????????id:?bk;
????????????????Rectangle{
????????????????????id:zezao;
????????????????????opacity:?0.9;
????????????????????anchors.fill:?parent;
????????????????????color:"#cccccc"
????????????????????Column{
????????????????????????width:?parent.width;
????????????????????????spacing:?2;
????????????????????????//WebView加載進度
????????????????????????Label{
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????id:txtprogress
????????????????????????????text:"WebKit加載進度:"+txtprogress_var
????????????????????????}

????????????????????????Rectangle{

????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:bartxt;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}

????????????????????????}

?????????????????????????//mainpage加載進度
????????????????????????Label{
????????????????????????????id:txtmainpage;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????text:"主頁面加載進度:"+txtmainpage_var
????????????????????????}

????????????????????????Rectangle{
????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:barmainpage;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}
????????????????????????}

????????????????????????//子頁面加載進度
????????????????????????Label{
????????????????????????????id:txtsubpage1;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????text:"首頁加載進度:"+txtmainpage_var
????????????????????????}

????????????????????????Rectangle{
????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:barsubpage1;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}
????????????????????????}

????????????????????}

????????????????????Behavior?on?opacity?{
??????????????????????NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}

????????????????????}
????????????????????onOpacityChanged:?{
????????????????????????if(opacity==0)
????????????????????????{
????????????????????????????bk.visible?=?false;
????????????????????????????bk.deleteLater();
????????????????????????????dbg("onOpacityChanged");
????????????????????????}
????????????????????}
????????????????}
????????????????//?禁止事件穿透
???????????????????MouseArea{
???????????????????????anchors.fill:?parent;
???????????????????????onPressed:{
????????????????????????????mouse.accepted?=?true
???????????????????????}
?????????????????????//??drag.target:?root??//?root可拖動
???????????????????}
???????????????????//外部調(diào)用刷新更新狀態(tài)?并且判斷是否全部加載成功
????????????????function?changgepro(type,pro)
????????????????{
????????????????????if(type?==?0)//webview加載進度
????????????????????{
????????????????????????bartxt.pass?=?pro;
????????????????????????txtprogress_var?=?pro;
????????????????????}else??if(type?==?1)//mainpage加載進度
????????????????????{
?????????????????????????barmainpage.pass?=?pro;
????????????????????????txtmainpage_var?=?pro;
????????????????????}else??if(type?==?2)//mainpage加載進度
????????????????????{
?????????????????????????barsubpage1.pass?=?pro;
????????????????????????txtsubpage1_var?=?pro;
????????????????????}
????????????????????if(txtprogress_var?==?100??&&?txtmainpage_var?==?100?&&?txtsubpage1_var?==100)
????????????????????{
????????????????????????oninitOk();

????????????????????}
????????????????}
????????????????//前臺頁面加載完畢后的行為
????????????????function?oninitOk()
????????????????{
????????????????????foot.visible?=?true;
????????????????????zezao.opacity?=?0;
?????????????????????benavShow?=?true;
????????????????????messaggeBox("狀態(tài)","加載完畢");
//?????????????????????bk.visible?=?false;
????????????????}



????????????}


????WebView{
????????//加載完畢?loading變成false
????????visible:?false;
????????id:mywebview;
????????objectName:?"webview"
??????????url:"file:///android_asset/index.html";
????????onLoadProgressChanged:?{

//????????????????txtprogress.text="WebKit加載進度:"+mywebview.loadProgress;
?????????????bk.changgepro(0,mywebview.loadProgress);
????????}
????????onLoadingChanged:?{

????????}
????}

????//頁頭
????property?alias?roothd:?hd
?????header:?ToolBar{
????????id:hd;
????????states:?[
????????????State?{
????????????????name:?"hide"
????????????????PropertyChanges?{
????????????????????target:?hd;opacity:0;height:0;width:0;
????????????????}
????????????????PropertyChanges?{
????????????????????target:?lisetview;opacity:0;rotation:360;height:0;
????????????????}
????????????}
????????]

????????transitions:?Transition?{
????????????//?Make?the?state?changes?smooth
????????????ParallelAnimation?{
????????????????NumberAnimation?{?duration:?500;?properties:?"opacity,x,contentY,height,width"?}
????????????????ColorAnimation?{?property:?"color";?duration:?888?}
????????????????NumberAnimation?{?duration:?888;?properties:?"rotation"?}


????????????}
????????}


????????height:dpH(60);
????????Text{
????????????text:"mywebview.loadProgress+"
????????????anchors.centerIn:?parent
????????????color:?"white"
????????????font.pixelSize:?dpW(18)
????????}

????????layer.enabled:?true
????????layer.effect:?DropShadow?{
????????????transparentBorder:?true//繪制邊框陰影
????????????color:?"#000000";
????????????radius:?dpH(15)
????????????id:drop;

????????????//cached:?true;
????????????horizontalOffset:?0;
????????????verticalOffset:?0;
????????????samples:?16;
????????????smooth:?true;


????????}
????}

????function??getdpistype()
????{?
????????console.log("SCALA:?"+Screen.pixelDensity*25.4/160)
????????console.log(Screen.pixelDensity)
????????var?curdpi?=?Screen.pixelDensity*25.4;
????????var??mydpi?=?curdpi.toFixed(0);
????????console.log(mydpi);
????????console.log("my?dpi?"+myapp.getdpi())
????????return?"MYDPI+"+mydpi;
????????if(mydpi>=480)
????????{
????????????console.log("XXHDPI");
????????????return?"XXHDPI";
????????}else??if(mydpi>=320)
????????{
????????????console.log("XHDPI");
????????????return?"XHDPI";
????????}else??if(mydpi>=240)
????????{
????????????console.log("HDPI");
????????????return?"HDPI";
????????}else??if(mydpi>=180)
????????{
????????????console.log("MDPI");
????????????return?"MDPI";
????????}else
????????{
????????????console.log("LDPI");
????????????return?"LDPI";
????????}


????}
????Loader{
????????id:mainpage;
????????asynchronous:?true
????????anchors.fill:?parent;
????????sourceComponent:?commapnpage;
????????onProgressChanged:?{
????????????bk.changgepro(1,progress*100);

????????}

????}
????Component{
????????id:commapnpage;
????????SwipeView{
????????????state:?"hide1"
????????????currentIndex:?tabindex

????????????onCurrentIndexChanged:?{
????????????????console.log("onCurrentIndexChanged:"+currentIndex);
????????????????tabindex?=?currentIndex;//導航欄的序號與這里同步,手動滑動觸發(fā)這里
????????????????if(currentIndex==1)
????????????????{
????????????????????tongxinlu.item.獲取好友列表();
????????????????}

????????????}


????????????//首頁
????????????Page{

????????????????id:gouzhen;
????????????????Loader{
????????????????????id:shouye
?????????????????????asynchronous:?true//異步加載組件
?????????????????????anchors.fill:?parent
????????????????????sourceComponent:?Page_gouzhen{}
????????????????????onProgressChanged:?{
????????????????????????bk.changgepro(2,shouye.progress*100);
????????????????????}


????????????????}
????????????}
????????//通訊錄
????????????Page{
????????????????id:tongxun;
????????????????Loader{
??????????????????asynchronous:?true
????????????????????id:tongxinlu;
?????????????????????anchors.fill:?parent;
????????????????????sourceComponent:?PageTongxunlv{}

????????????????}
????????????}



????????}
????}

????//當前選中的導航序號
???property?var??benavShow??:false;//是否顯示底部導航
????property?int?tabindex:?mainpage.item.currentIndex
????footer:?Row{
????????visible:?benavShow;

????????id:foot;
????????width:benavShow??parent.width:0;
????????height:benavShow??width/5*0.75:0;

????????Repeater{
????????????id:rep
????????????delegate:?NavNewDelegate{
????????????????width:?benavShow?parent.width/5:0;
????????????????height:benavShow?width*0.75:0;
????????????????rotation:benavShow?0:Math.random()*360
????????????????id:navitem;
???????????????Behavior?on?width?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}
???????????????Behavior?on?height?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}
???????????????Behavior?on?rotation?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}

???????????????onClick:?{
????????????????????dbg("Delegate傳遞過來的下標:"+index+tabindex);
????????????????????console.log("Delegate傳遞過來的下標:"+index+tabindex);

????????????????}

????????????}
????????????model:NavNewModel{id:model1}
????????}
????}
????Keys.enabled:?true;
????Keys.onReleased:??{
????????console.log("key"+event.key);
????????if(event.key==Qt.Key_Back)
????????{
????????????console.log("back");
????????}
????}
}
****關鍵代碼??QML和C++以及webview中的js庫交互的部分
import?QtQuick?2.7
import?QtQuick.Controls?1.4?as?Old
import?QtQuick.Controls?2.0
import?QtQuick.Layouts?1.0
import?QtQuick.Layouts?1.1
import?QtQuick.Window?2.0
import?QtQuick.Dialogs?1.2
import?QtQuick.Controls.Styles?1.4
import?QtQuick.Controls.Material?2.0
import?QtQuick.Controls.Universal?2.0
import?QtGraphicalEffects?1.0
import?QtQuick.Particles?2.0
import?QtWebSockets?1.0
import?"../"
Item?{
????property?alias?mview:?view
????objectName:?"FList";
????????anchors.fill:?parent;
????????id:gen;
????????ListView{
????????????id:view;

?????????????anchors.fill:?parent;
????????????delegate:?ListTongxunlvDelegate{
????????????}
????????????model:?ListTongxunlvModel{
????????????????id:model;
????????????}
????????????Component.onCompleted:?{
????????????????????myapp.setTXL(gen);
?????????????????????console.log("cout:"+model.count);
?????????????????????model.append({name:"代碼統(tǒng)計行數(shù):"+model.count,tip:"代碼生成的行"});
????????????????}
????????}

????????//專門被C++調(diào)用的函數(shù)
????????function?刷新好友列表(x)
????????{
????????????console.log("get?c++?*****"+x)
????????????root.messaggeBox("刷新好友列表:",x);

????????}

????function?獲取好友列表()
????{

????????mywebview.runJavaScript("獲取好友列表()",function(result){
????????????for(var?x?in??result)
????????????{
????????????????root.dbg(result[x]);
????????????}
?????????????root.dbg(result);


????????});
????}



}
**********HTML代碼?使用裸頁面只為加載JS功能
h
*****js代碼
var?msocket;
mui.init({})
mui.ready(lod);
function???登錄成功(arg1)
{
//??$.post("http://localhost:8080/loging?p1="+arg1,?function(data){
//??????alert("Data?Loaded:?"?+?data.name);
//??},"json");
?$.post("http://localhost:8080/loging?p1="+arg1);
}
function???發(fā)送到C加加(path,arg1)
{
//??$.post("http://localhost:8080/loging?p1="+arg1,?function(data){
//??????alert("Data?Loaded:?"?+?data.name);
//??},"json");
??$.post("http://localhost:8080/"+path+"?p1="+arg1);
}
var??isloging?=?false;
function??lod(){


????????alert("ok");

????????console.log("模擬器連接");
//??????????msocket?=?io.connect('ws://10.0.2.2:8081',?{?'reconnect':?true?});//模擬器訪問局域網(wǎng)
????????msocket?=?io.connect('ws://192.168.0.101:8081',?{?'reconnect':?true?});//模擬器訪問局域網(wǎng)

????????msocket.on('connect',function(){
????????????alert("連接成功;");
????????????isloging?=?true;
????????????msocket.emit("denglu","admin","123",function(callbackdata){

????????????????alert("登錄結果:"+callbackdata);
????????????})
????????????登錄成功(1);

????????});


????//正在連接
????msocket.on('connecting',function(){
????????alert("正在連接");
????});

????//連接超時
????msocket.on('connect_timeout',function(){
????????console.log("connect_timeout");
????});


????//連接失敗
????msocket.on('connect_failed',function(){
????????alert("連接失敗");
????});

????//錯誤發(fā)生?并且無法被其他事件類型所處理
????msocket.on('error',function(data){
????????alert("錯誤發(fā)生?并且無法被其他事件類型所處理");

????});



????//重連失敗
????msocket.on('reconnect_failed',function(){
????????alert("reconnect_failed");
????});

????//成功重連
????msocket.on('reconnect',function(TheNumber){
????????alert("reconnectOk"+TheNumber);
????});

????//正在重連
????msocket.on('reconnecting',function(TheNumber){
????????console.log("reconnecting"+TheNumber);

????});


}


function??Testfunc()
{
//??alert("get")
????return?123;
}
//對應QML代碼:
//function?獲取好友列表()
//????{
//
?//???????mywebview.runJavaScript("獲取好友列表()",function(result){
//????????????for(var?x?in??result)
//????????????{
??//??????????????root.dbg(result[x]);
??//??????????}
?//????????????root.dbg(result);
//
//
//????????});
??//??}
var??mdata;
function?獲取好友列表()
{

????if(isloging==false)
????{
????????alert("暫未登錄,無法拉取好友");
????????return?"";
????}?
????????alert("獲取好友中");



?????????????msocket.emit("獲取好友",/*"獲取好友",*/function(data){
????????//傳過來的是字符串?不需要轉換了??交給QtQuick轉換吧?一樣
????????//不對?貌似qtquick可以直接解析對象
????????console.log(data)
????????發(fā)送到C加加("Flist",data);

????????mdata?=?data;
//??????console.log(JSON.parse(data))
????????????});

????//由于是異步的?因此可能先返回了?才接收到服務器發(fā)過來的數(shù)據(jù)?解決方式很多?這里用同步方式
????alert("最后層返回");
????return?mdata;//此返回值無用?真正的返回值是由服務器轉發(fā)過來經(jīng)過?發(fā)送到C加加("Flist",data)?轉發(fā)給QML
}
本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或將催生出更大的獨角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉型技術解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關鍵字: 汽車 人工智能 智能驅動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質量流程IT總裁陶景文發(fā)表了演講。

關鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權最終是由生態(tài)的繁榮決定的。

關鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質量發(fā)展策略,塑強核心競爭優(yōu)勢...

關鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術學會聯(lián)合牽頭組建的NVI技術創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術創(chuàng)新聯(lián)...

關鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關鍵字: BSP 信息技術
關閉
關閉