| Tutorial | Classes | Functions | QSA Developer | Language | Library | Qt API Qt Script for Applications

[Prev: Identifiers, Variables and Constants] [Home] [Next: Class Properties]

Classes and Methods

Qt Script is a fully object oriented language. Each form in a Qt Script for Applications GUI application is represented by a class. Non-GUI classes can be defined as shown below in the source files of a project.

Example:

    class Circle {
        var _x;
        var _y;
        var _r;

        function Circle( x, y, r )
        {
            this._x = x;
            this._y = y;
            this._r = r;
        }
        function setX( x ) { this._x = x; }
        function setY( y ) { this._y = y; }
        function setR( r ) { this._r = r; }
        function x() { return this._x; }
        function y() { return this._y; }
        function r() { return this._r; }
    }

    class ColorCircle extends Circle {
        var _rgb;

        function ColorCircle( x, y, r, rgb )
        {
            super( x, y, r );
            this._rgb = rgb;
        }
        function setRgb( rgb ) { this._rgb = rgb; }
        function rgb() { return this._rgb; }
    }

A class's constructor is the function which has the same (case-sensitive) name as the class itself. The constructor should not contain an explicit return statement; it will return an object of its type automatically.

The class's member variables are declared with var, and its member functions (methods) with function.

The object instance itself is referred to using the this operator. Inside a member function of a class, member variables and member functions are accessed with an explicit this (e.g. this._x = x;)

Forms are constructed automatically by Qt Script for Applications. If code must be executed at construction time it should be placed in a function called init() which takes no arguments and returns nothing. Qt Script for Applications will call this function when the form is created, because by default Qt Script for Applications connects the form's init() signal to the init() slot if such a slot exists.

Qt Script does not have destructor functions. However, if you define a function called destroy() (which takes no arguments and returns nothing) in a form, Qt Script for Applications will call this function when the form is closed, because by default Qt Script for Applications connects the form's destroyed() signal to the destroy() slot if such a slot exists

The following language features are still under discussion and are therefore subject to change. They might not be implemented fully, or might even disappear.

Qt Script supports single inheritance, and if a class inherits from another class, the superclass's constructor can be called with super().

Functions and variables may specify the types of their arguments and return types if desired. For example:

    class ColorCircle extends Circle {
        var _rgb : Color;

        function ColorCircle(
            x : Integer, y : Integer, r : Integer,
            rgb : Color )
        {
            super( x, y, r );
            this._rgb = rgb;
        }
        function setRgb( rgb : Color ) { this._rgb = rgb; }
        function rgb() : Color { return this._rgb; }
    }

See also class, function, Function type, function operator.

Qualified Names

When you declare an object of a particular type, the object itself becomes, in effect, a namespace. For example, in Qt Script for Applications there is a function called Math.sin(). If you wanted to have a sin() function in your own class that wouldn't be a problem, because objects of your class would call the function using the object.function() syntax. The period is used to distinguish the namespace a particular identifier belongs to.

For example, in a Qt Script for Applications GUI application, every form belongs to the Application object, and every widget belongs to a form. This can lead to some rather lengthy code, for example Application.Dialog.ListBox.count. Such long names can often be shortened, for example, within a signal handler, e.g. this.ListBox.count. In practice, Qt Script for Applications is intelligent enough to work out the fully qualified name, so the code you would actually write is simply ListBox.count. The only time that you need to qualify your names is when an unqualified name is ambiguous.

Class Properties

[Prev: Identifiers, Variables and Constants] [Home] [Next: Class Properties]


Copyright © 2001-2002 TrolltechTrademarks
QSA version 1.0.0-beta1