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>

    QAXFACTORY_DEFAULT( QTetrax,
                "{852558AD-CBD6-4f07-844C-D1E8983CD6FC}",
                "{2F5D0068-772C-4d1e-BCD2-D3F6BC7FD315}",
                "{769F4820-9F28-490f-BA50-5545BD381DCB}",
                "{5753B1A8-53B9-4abe-8690-6F14EC5CA8D0}",
                "{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("Tetrax");
            a.setMainWidget(tetrax);
            tetrax->setCaption("Qt Example - Tetrax");
            tetrax->show();
        }
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
        CLASSID="CLSID:852558AD-CBD6-4f07-844C-D1E8983CD6FC"
        CODEBASE=http://www.trolltech.com/demos/tetrax.cab>
        <PARAM NAME="score" VALUE="0">
    [Object not available! Did you forget to build and register the server?]
    </object>
An HTML button is added to start the game.
    <form>
        <input type="button" value="Start Game..."
            onClick="QTetrax.startGame()">
    </form>
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!" );
    }
    </SCRIPT>

See also The QAxServer Examples.


Copyright © 2005 TrolltechTrademarks
Qt 3.3.6