QT和flex swf進行相互調用和互動

來源:互聯網
上載者:User

 

  1. 在專案檔xxxx.pro中加入 CONFIG

    += qaxcontainer

  2. 在視窗中使用如下代碼,嵌入並播放flash
    <br />//中文編碼設定<br />QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));<br />//<br />QString applicationPath =QCoreApplication::applicationDirPath();<br /> swf = new QAxWidget(0,0);<br /> //這個show的方法要放在這裡,否則會產生初始化swf的時候,大小有問題的情況<br /> swf->show();<br /> //<br /> swf->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));<br /> swf->dynamicCall("LoadMovie(int,QString)",0,applicationPath+"/Resources/Main.swf");<br /> swf->dynamicCall("Play");

     

  • 讓QT的程式調用swf中的方法:

    • 在qt的主視窗中定義一個按鈕。按鈕的click事件如下:
    • void MainWindow::on_pushButton_clicked()<br />{<br /> //QMessageBox::warning(NULL, "warning","on click", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);<br /> QString funName="callWithEXE";<br /> QString arg = QObject::tr("哦喲!我是QT傳給flex的內容");<br /> QString callMethod ="<invoke name=/"" + funName + "/" returntype=/"xml/"><arguments><string>" + arg + "</string></arguments></invoke>";<br /> swf->dynamicCall("CallFunction(string)",callMethod);<br />} 
    • 在flex的as檔案中定義介面程式如下:
      private function init():void{<br />//這是外部介面,讓C#可以調用<br />ExternalInterface.addCallback("callWithEXE",callWithEXE);<br />}

       

    • 在flex的as檔案中定義程式的實現方法
      public function callWithEXE(str:String):void{<br />Alert.show(str,"內容顯示的是");<br />}
    • 上面的程式碼片段,完成了如此情境:在qt的主視窗有個按鈕,點擊按鈕後,在flex裡面顯示出一個alert視窗,內容為:哦喲,我是QT傳遞給flex的內容
    • 讓swf中調用QT中的方法:

      • 在flex中定義一個按鈕。訪問QT中的方法,並獲得一個傳回值
        protected function button1_clickHandler(event:MouseEvent):void<br />{<br />// TODO Auto-generated method stub<br />if( ExternalInterface.available){<br />//Alert.show("ExternalInterface 可用。可以調用外殼方法")<br />//調用外部程式SayHello方法,並傳入方法參數"Hunk",輸出方法傳回值result<br />var strReturn:String = ExternalInterface.call("SayHello", "Hunk","roamer");<br />Alert.show(strReturn);<br />}else{<br />Alert.show("ExternalInterface 不可用。")<br />}<br />}

         

      • 在QT中定義一個slot,和來自flex中的方法產生signal進行串連。由於as3運行時候,在flex中的ExternalInterface.call會產生一個signal,形式是FlashCall(

        QString

        ),其中QString是flex方法裡面傳來的參數"Hunk","roamer".

      • 定義一個slot,在標頭檔中定義。
        public slots:void SayHello(QString message);

         

      • 在QT的代碼中串連一個slot
        //連結一個槽,使得swf中調用後,使用這個槽<br /> connect(swf,SIGNAL(FlashCall(QString)),this,SLOT(SayHello(const QString&)));

      • 實現slot中的方法
        void MainWindow::SayHello(QString message)<br />{<br /> QMessageBox::warning(NULL,QObject::tr( "我是swf傳遞過來的內容"),message, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);<br /> QString rtnString =QObject::tr("<string>哦也!我是qt返回的內容</string>");<br /> swf->dynamicCall("SetReturnValue(string)",rtnString);<br />}

        注意:flex中傳遞過來的內容是一個包含方法名和參數列表的xml。同樣:如果要讓QT給flex代碼傳回值。傳回值也必須是xml字串(

        "<string>哦也!我是qt返回的內容</string>"

        ;

        )。否則不能獲得

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.