|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles Qt Script for Applications

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

Classes and Methods

Qt Script is a fully object oriented language. Classes can be defined as shown below in the source files of a project.

Example:

    class Circle {
        var m_x;
        var m_y;
        var m_r;

        function Circle( posx, posy, radius )
        {
            m_x = posx;
            m_y = posy;
            m_r = radius;
        }
        function setX( posx ) { m_x = posy; }
        function setY( posy ) { m_y = posy; }
        function setR( radius ) { m_r = radius; }
        function x() { return m_x; }
        function y() { return m_y; }
        function r() { return m_r; }
    }

    class ColorCircle extends Circle {
        var m_rgb;

        function ColorCircle( posx, posy, radius, rgbcolor)
        {
            Circle( posx, posy, radius );
            m_rgb = rgbcolor;
        }
        function setRgb( rgbcolor ) { m_rgb = rgbcolor; }
        function rgb() { return m_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. Qt Script does not have a destructor function (a function that is called when the class is destroyed), for a class.

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 can be accessed with an explicit this (e.g. this.x = posx;). This is not required, but can sometimes help to increase visibility.

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

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 application object belongs to the Application object. 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-2006 TrolltechTrademarks
QSA version 1.1.5