OpenGL 範例 (ActiveQt)

The OpenGL example demonstrates the use of the default factory and QAxFactory::isServer (), and the implementation of an additional COM interface using QAxBindable and QAxAggregated . The server executable can run both as an ActiveX server and as a stand-alone application.

The ActiveX control in this example uses the QGlWidget class in Qt to render an OpenGL scene in an ActiveX. The control exposes a few methods to change the scene.

The application uses the default factory as provided by the QAXFACTORY_DEFAULT macro to expose the GLBox 小部件作為 ActiveX 控製。

#include <QAxFactory>
QAXFACTORY_DEFAULT( GLBox,
                    "{5fd9c22e-ed45-43fa-ba13-1530bb6b03e0}",
                    "{33b051af-bb25-47cf-a390-5cfd2987d26a}",
                    "{8c996c29-eafa-46ac-a6f9-901951e765b5}",
                    "{2c3c183a-eeda-41a4-896e-3d9c12c3577d}",
                    "{83e16271-6480-45d5-aaf1-3f40b7661ae4}"
                  )
					

實現為 main 初始化 QApplication 對象,和使用 QAxFactory::isServer() 以確定創建和展示應用程序界麵是否閤適。

/*
  The main program is here.
*/
int main( int argc, char **argv )
{
    QApplication::setColorSpec( QApplication::CustomColor );
    QApplication a(argc,argv);
    if ( !QGLFormat::hasOpenGL() ) {
        qWarning( "This system has no OpenGL support. Exiting." );
        return -1;
    }
    if ( !QAxFactory::isServer() ) {
        GLObjectWindow w;
        w.resize( 400, 350 );
        w.show();
        return a.exec();
    }
    return a.exec();
}
					

The GLBox 類繼承自兩者 QGLWidget class to be able to render OpenGL , and from QAxBindable .

#include <QAxBindable>
class GLBox : public QGLWidget,
              public QAxBindable
{
    Q_OBJECT
					

類重實現 QAxBindable::createAggregate () 函數從 QAxBindable 以返迴指針指嚮 QAxAggregated 對象。

public:
    GLBox( QWidget* parent, const char* name = 0 );
    ~GLBox();
    QAxAggregated *createAggregate();
public slots:
    void                setXRotation( int degrees );
					

The rest of the class declaration and the implementation of the OpenGL rendering is identical to the original "box" example.

實現文件為 GLBox 類包括 objsafe.h 係統頭,在那裏 IObjectSafety COM 接口有定義。

#include <objsafe.h>
					

ObjectSafetyImpl 的聲明是使用多繼承以子類化 QAxAggregated 類,並實現 IObjectSafety 接口。

class ObjectSafetyImpl : public QAxAggregated,
                         public IObjectSafety
{
public:
					

類聲明默認構造函數,並實現 queryInterface 函數以支持 IObjectSafety 接口。

    ObjectSafetyImpl() {}
    long queryInterface( const QUuid &iid, void **iface )
    {
        *iface = 0;
        if ( iid == IID_IObjectSafety )
            *iface = (IObjectSafety*)this;
        else
            return E_NOINTERFACE;
        AddRef();
        return S_OK;
    }
					

由於各 COM 接口繼承 IUnknown the QAXAGG_IUNKNOWN 宏用於提供默認實現為 IUnknown 接口。宏定義將所有調用委托給 QueryInterface , AddRef and Release 到由 controllingUnknown() 函數返迴的接口。

    QAXAGG_IUNKNOWN;
					

實現為 IObjectSafety 接口為調用者提供支持和啓用安全選項的有關信息,並返迴 S_OK 為所有調用以指示 ActiveX 控件是安全的。

    HRESULT WINAPI GetInterfaceSafetyOptions( REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions )
    {
        *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
        *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
        return S_OK;
    }
    HRESULT WINAPI SetInterfaceSafetyOptions( REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions )
    {
        return S_OK;
    }
};
					

實現為 createAggregate() 函數僅僅返迴新 ObjectSafetyImpl 對象。

QAxAggregated *GLBox::createAggregate()
{
    return new ObjectSafetyImpl();
}
					

要構建範例必須先構建 QAxServer 庫。然後運行 qmake 和 make 工具在 examples/activeqt/wrapper .

The demonstration 要求 WebBrowser 支持 ActiveX 控件,並啓用腳本。

相比其它 QAxServer 範例,Internet Explorer 不會打開對話框詢問用戶是否應該允許運行 GLBox 控件腳本 (瀏覽器的準確行為從屬 "Internet 選項" 對話框中的安全設置)。

<SCRIPT LANGUAGE="JavaScript">
function setRot( form )
{
    GLBox.setXRotation( form.XEdit.value );
    GLBox.setYRotation( form.YEdit.value );
    GLBox.setZRotation( form.ZEdit.value );
}
</SCRIPT>
<p />
An OpenGL scene:<br />
<object ID="GLBox" CLASSID="CLSID:5fd9c22e-ed45-43fa-ba13-1530bb6b03e0"
CODEBASE="http://qt.nokia.com/demos/openglax.cab">
[Object not available! Did you forget to build and register the server?]
</object><br />
<form>
Rotate the scene:<br />
X:<input type="edit" ID="XEdit" value="0" /><br />
Y:<input type="edit" name="YEdit" value="0" /><br />
Z:<input type="edit" name="ZEdit" value="0" /><br />
<input type="button" value="Set" onClick="setRot(this.form)" />
</form>
					

文件: