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

# A TQt example as a scriptable ActiveX control (executable)

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

It demonstrates the use of the default factory provied by the TQAXFACTORY_DEFAULT macro, and of TQAxFactory::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 TQAxFactory is used through the TQAXFACTORY_DEFAULT macro, and exports the TQTetrax object specifying the five unique IDs required by COM to instantiate and communicate with the server.

        #include "qtetrax.h"
        #include "qdragapp.h"
        #include "ntqfont.h"

        #include <qaxfactory.h>

        TQAXFACTORY_DEFAULT( TQTetrax,
                    "{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 TQApplication 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 )
        {
            TQApplication::setColorSpec( TQApplication::CustomColor );
            TQDragApplication a(argc,argv);

            TQTetrax *tetrax = 0;
            if ( !TQAxFactory::isServer() ) {
                tetrax = new TQTetrax;
                tetrax->setCaption("Tetrax");
                a.setMainWidget(tetrax);
                tetrax->setCaption("TQt 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 TQAxServer 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 TQt's layout engine.

        <object ID="TQTetrax" 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="TQTetrax.startGame()">
        </form>

And an event handler for the `gameOver()` event is implemented in JavaScript to display a simple message box.

        <SCRIPT LANGUAGE=JavaScript>
        function TQTetrax::gameOver()
        {
            alert( "GameOver!" );
        }
        </SCRIPT>

See also The TQAxServer Examples.