OpenGL Example (ActiveQt)▲
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 QAxFactory through the QAXFACTORY_BEGIN(), QAXCLASS() and QAXFACTORY_END() macros to expose the GLBox widget as an ActiveX control.
#include <QAxFactory>
QAXFACTORY_BEGIN(
"{2c3c183a-eeda-41a4-896e-3d9c12c3577d}"
, // type library ID
"{83e16271-6480-45d5-aaf1-3f40b7661ae4}"
) // application ID
QAXCLASS(GLBox)
QAXFACTORY_END()
The implementation of main initializes the QApplication object, and uses QAxFactory::isServer() to determine whether or not it is appropriate to create and show the application interface.
/*
The main program is here.
*/
int
main(int
argc, char
*
argv[])
{
QApplication a(argc,argv);
if
(QOpenGLContext::
openGLModuleType() !=
QOpenGLContext::
LibGL) {
qWarning("This system does not support OpenGL. Exiting."
);
return
-
1
;
}
if
(!
QAxFactory::
isServer()) {
GLObjectWindow w;
w.resize(400
, 350
);
w.show();
return
a.exec();
}
return
a.exec();
}
The GLBox class inherits from both the QOpenGLWidget class to be able to render OpenGL, and from QAxBindable.
#include <QAxBindable>
class
GLBox : public
QOpenGLWidget,
public
QOpenGLFunctions_1_1,
public
QAxBindable
{
Q_OBJECT
Q_CLASSINFO("ClassID"
, "{5fd9c22e-ed45-43fa-ba13-1530bb6b03e0}"
)
Q_CLASSINFO("InterfaceID"
, "{33b051af-bb25-47cf-a390-5cfd2987d26a}"
)
Q_CLASSINFO("EventsID"
, "{8c996c29-eafa-46ac-a6f9-901951e765b5}"
)
The class reimplements the QAxBindable::createAggregate() function from QAxBindable to return the pointer to a QAxAggregated object.
public
:
explicit
GLBox(QWidget *
parent, const
char
*
name =
nullptr
);
virtual
~
GLBox();
QAxAggregated *
createAggregate() override
;
public
slots:
void
setXRotation(int
degrees);
The implementation file of the GLBox class includes the objsafe.h system header, in which the IObjectSafety COM interface is defined.
#include <objsafe.h>
A class ObjectSafetyImpl is declared using multiple inheritance to subclass the QAxAggregated class, and to implement the IObjectSafety interface.
class
ObjectSafetyImpl : public
QAxAggregated,
public
IObjectSafety
{
public
:
The class declares a default constructor, and implements the queryInterface function to support the IObjectSafety interface.
explicit
ObjectSafetyImpl() =
default
;
long
queryInterface(const
QUuid &
amp;iid, void
**
iface) override
{
*
iface =
nullptr
;
if
(iid !=
IID_IObjectSafety)
return
E_NOINTERFACE;
*
iface =
static_cast
&
lt;IObjectSafety*&
gt;(this
);
AddRef();
return
S_OK;
}
Since every COM interface inherits IUnknown the QAXAGG_IUNKNOWN macro is used to provide the default implementation of the IUnknown interface. The macro is defined to delegate all calls to QueryInterface, AddRef and Release to the interface returned by the controllingUnknown() function.
QAXAGG_IUNKNOWN;
The implementation of the IObjectSafety interface provides the caller with information about supported and enabled safety options, and returns S_OK for all calls to indicate that the ActiveX control is safe.
HRESULT WINAPI GetInterfaceSafetyOptions(REFIID riid, DWORD *
pdwSupportedOptions, DWORD *
pdwEnabledOptions) override
{
Q_UNUSED(riid);
*
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) override
{
Q_UNUSED(riid);
Q_UNUSED(pdwSupportedOptions);
Q_UNUSED(pdwEnabledOptions);
return
S_OK;
}
}
;
The implementation of the createAggregate() function just returns a new ObjectSafetyImpl object.
QAxAggregated *
GLBox::
createAggregate()
{
return
new
ObjectSafetyImpl();
}
To build the example you must first build the QAxServer library. Then run qmake and your make tool in examples/activeqt/wrapper.
The demonstration requires your WebBrowser to support ActiveX controls, and scripting to be enabled.
In contrast to the other QAxServer examples Internet Explorer will not open a dialog box to ask the user whether or not the scripting of the GLBox control should be allowed (the exact browser behaviour depends on the security settings in the Internet Options dialog).
&
lt;SCRIPT LANGUAGE=
"JavaScript"
&
gt;
function setRot( form )
{
GLBox.setXRotation( form.XEdit.value );
GLBox.setYRotation( form.YEdit.value );
GLBox.setZRotation( form.ZEdit.value );
}
&
lt;/
SCRIPT&
gt;
&
lt;p /&
gt;
An OpenGL scene:&
lt;br /&
gt;
&
lt;object ID=
"GLBox"
CLASSID=
"CLSID:5fd9c22e-ed45-43fa-ba13-1530bb6b03e0"
CODEBASE=
"http://qt.nokia.com/demos/openglax.cab"
&
gt;
[Object not
available!
Did you forget to build and
register
the server?]
&
lt;/
object&
gt;&
lt;br /&
gt;
&
lt;form&
gt;
Rotate the scene:&
lt;br /&
gt;
X
:&
lt;input type=
"edit"
ID=
"XEdit"
value=
"0"
/&
gt;&
lt;br /&
gt;
Y
:&
lt;input type=
"edit"
name=
"YEdit"
value=
"0"
/&
gt;&
lt;br /&
gt;
Z
:&
lt;input type=
"edit"
name=
"ZEdit"
value=
"0"
/&
gt;&
lt;br /&
gt;
&
lt;input type=
"button"
value=
"Set"
onClick=
"setRot(this.form)"
/&
gt;
&
lt;/
form&
gt;