Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

A Qt example as a scriptable ActiveX control (executable)

This example shows how to turn an existing Qt application into an ActiveX control server. The ActiveX control is based on the Qt tetrix example.

It demonstrates the use of the default factory provied by the QAXFACTORY_DEFAULT macro, and of QAxFactory::isServer().

The code changes for the tetrix GUI are minimal (a property score, a signal gameOver and a slot startGame) and provide a better scripting interface for the use of the control in a web environment.

The implementation of the ActiveX server functionality is only in the tetrax.cpp file. The default implementation of the QAxFactory is used through the QAXFACTORY_DEFAULT macro, and exports the QTetrax object specifying the five unique IDs required by COM to instantiate and communicate with the server.

    #include "qtetrax.h"
    #include "qdragapp.h"
    #include "qfont.h"

    #include <qaxfactory.h>

                "{DE2F7CE3-CFA7-4938-A9FC-867E2FEB63BA}" )
The main entry point method instantiates a QApplication object, and creates the GUI only if the program is not running as an ActiveX server (ie. the program has been started by the user, not by COM).
    int main( int argc, char **argv )
        QApplication::setColorSpec( QApplication::CustomColor );
        QDragApplication a(argc,argv);

        QTetrax *tetrax = 0;
        if ( !QAxFactory::isServer() ) {
            tetrax = new QTetrax;
            tetrax->setCaption("Qt Example - Tetrax");
The server enters the application event loop, and destroys the GUI before exiting.
        int res = a.exec();
        delete tetrax;
        return res;

To build the example you must first build the QAxServer library. Then run qmake and your make tool in examples/tetrix.

The demonstration requires your Web browser to support ActiveX controls, and scripting to be enabled.

The "Tetrix" control is embedded using the <object> tag. Note that the dimensions of the control are provided explicitely, as the control itself does not use Qt's layout engine.

    <object ID="QTetrax" width=550 height=370
        <PARAM NAME="score" VALUE="0">
    [Object not available! Did you forget to build and register the server?]
An HTML button is added to start the game.
        <input type="button" value="Start Game..."
And an event handler for the gameOver() event is implemented in JavaScript to display a simple message box.
    <SCRIPT LANGUAGE=JavaScript>
    function QTetrax::gameOver()
        alert( "GameOver!" );

See also The QAxServer Examples.

Copyright © 2005 TrolltechTrademarks
Qt 3.3.7