QT中使用ActiveX控件、QAxBase、QAxWidget

QaxContainer模塊是訪問ActiveX控件和COM對象的一個Windows擴展。QAxContainer模塊是ActiveQt構架的一部分。它提供一個庫,由擔當ActiveX控件容器角色的QWidget的子類Q...

 

QaxContainer模塊是訪問ActiveX控件和COM對象的一個Windows擴展。

    QAxContainer模塊是ActiveQt構架的一部分。它提供一個庫,由擔當ActiveX控件容器角色的QWidget的子類QAxWidget 和 用來簡化訪問非可視化COM對象的QObject的子類QAxWidget 實現。通過類QAxScript, QAxScriptManager和QAxScriptEngine可以解釋嵌入的COM對象,還有一個工具集使得編程訪問COM對象更容易。

    模塊由6個類構成:
        1. QAxBase 是一個提供用來初始化和訪問一個COM對象及ActiveX控件的API的抽象類。
        2. QAxObject是一個包裝了COM對象的QObject。
        3. QAxWidget 是一個包裝了ActiveX控件的QWidget。
        4. QAxScriptManager, QAxScript 和 QAxScriptEngine provide an interface to the Windows Script Host.

Some example applications that use standard ActiveX controls to provide high-level user interface functionality are provided.
提供了一些使用標準ActiveX控件來提供高級用戶界面功能的示例。
The QAxContainer module is part of the Qt Desktop Edition for Windows. It is not part of the Qt Open Source Edition.
=====================================================

1. 使用庫

    構造使用COM對象和ActiveX控件的Qt應用程序,需要向.pro文件中加入
            CONFIG += qaxcontainer
來連接到QAxContainer模塊

1.1. 配置QAxContainer應用程序

    QaxContainer庫是靜態的,因此使用這個模塊的時候不需要重新分配任何額外的文件。但要注意,你所使用的提供ActiveX服務的二進制文件必須被安裝在目標系統中,因此你需要把它們裝在你的發佈包中並在你的應用程序安裝過程中爲它們註冊。


2. 初始化COM對象

     可以通過使用QAxBase::setControl() 或 直接把對象的名字傳到QAxBase子類的構造器中 來初始化一個COM對象。

     控件能通過多種格式指定,但最快且功能最強的格式是直接使用對象的Class ID(CLSID)。 Class ID能考慮到這個對象涉及別的機器時信息的變化,而且能爲需要license的控件包括一個license key。

2.1. 典型的錯誤信息

    ActiveQt在運行中遇到錯誤時會向debug output顯示錯誤信息。通常要在調試器下運行你的程序來查看這些信息(e.g. in Visual Studio's Debug output)。

    -- Requested control could not be instantiated
                The control requested in QAxBase::setControl() is not installed on this system, or is not accessible for the current user.     The control might require administrator rights, or a license key. If the control is licensed, pass the license key to QAxBase::setControl as documented.


3. 訪問對象API

模塊提供了訪問COM對象的Qt API來取代COM的數據類型。

有4種方法去調用訪問COM對象的API:
         • Generating a C++ namespace
         • Call-by-name
         • Through a script engine
         • Using the native COM interfaces

3.1. 生成 C++ Namespace

    用dumpcpp 工具可以爲想要訪問的類型庫生成一個C++名空間。需要手動對你要用的類型庫使用這個工具, 或者也可以通過向.pro文件中的變量TYPELIBS添加類型庫來把它整合到編譯系統中:
            TYPELIBS = file.tlb
注意,dumpcpp不一定能列出類型庫中所有的API。
把生成的頭文件包含進你的代碼中,通過生成的C++類來訪問對象API。 更多信息可以參考示例Qutlook。

3.2. Call-by-Name

    用QAxBase::dynamicCall()、QAxBase::querySubObject() 和QObject::setProperty()、QObject::property() 能通過名字調用COM對象的方法和屬性。用dumpdoc工具能獲得COM對象和他的字對象的Qt API文檔。注意不是所有的COM對象的API是可用的。
更多請參看示例Webbrowser。

3.3. Calling Function Through a Script Engine

    Qt應用程序能使用安裝在系統上的任何ActiveScript engine。Script engine能運行腳本代碼去訪問COM對象。

    使用script engine時,用QAxScriptManager::addObject()註冊你想通過腳本訪問的COM對象,用QAxScriptManager::load()把script代碼裝入引擎。接着就可以用QAxScriptManager::call() 或 QAxScript::call()來調用script函數。

    COM對象的API結束了腳本對所使用的腳本語言的依賴。
    ActiveX Test Container 示範瞭如何裝載腳本文件。

3.4. Calling a Function Using the Native COM Interfaces

    上面的方法都不能訪問的COM對象函數,可以直接用QAxBase::queryInterface()去查詢COM接口。它可以獲得控件的類型庫中用#import標示的各個接口類的C++定義;更多的細節請看你的編譯器手冊。

3.5. 典型的錯誤信息

    ActiveQt在運行中遇到錯誤的時候會向debug output顯示錯誤信息。通常你必須在調試器下運行你的程序來查看這些信息(e.g. in Visual Studio's Debug output)。

    -- QAxBase::internalInvoke: No such method
            A QAxBase::dynamicCall() failed - the function prototype did not match any function available in the object's API.

    -- Error calling IDispatch member: Non-optional parameter missing
            A QAxBase::dynamicCall() failed - the function prototype was correct, but too few parameters were provided.

    -- Error calling IDispatch member: Type mismatch in parameter n
            A QAxBase::dynamicCall() failed - the function prototype was correct, but the paramter at index n was of the wrong type and could not be coerced to the correct type.

    -- QAxScriptManager::call(): No script provides this function
            You try to call a function that is provided through an engine that doesn't provide introspection (ie. ActivePython or ActivePerl). You need to call the function directly on the respective QAxScript object. 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章