QtCanvasItem Class Reference

The QtCanvasItem class provides an abstract graphic object on a QtCanvas. More...

#include <QtCanvasItem>

Inherited by QtCanvasPolygonalItem, QtCanvasSprite, and QtCanvasText.

Public Types

Public Functions

Protected Functions


Detailed Description

The QtCanvasItem class provides an abstract graphic object on a QtCanvas.

A variety of QtCanvasItem subclasses provide immediately usable behaviour. This class is a pure abstract superclass providing the behaviour that is shared among all the concrete canvas item classes. QtCanvasItem is not intended for direct subclassing. It is much easier to subclass one of its subclasses, e.g. QtCanvasPolygonalItem (the commonest base class), QtCanvasRectangle, QtCanvasSprite, QtCanvasEllipse or QtCanvasText.

Canvas items are added to a canvas by constructing them and passing the canvas to the canvas item's constructor. An item can be moved to a different canvas using setCanvas().

Items appear on the canvas after their show() function has been called (or setVisible(true)), and after update() has been called. The canvas only shows items that are visible, and then only if update() is called. If you created the canvas without passing a width and height to the constructor you'll also need to call resize(). Since the canvas background defaults to white and canvas items default to white, you may need to change colors to see your items.

A QtCanvasItem object can be moved in the x(), y() and z() dimensions using functions such as move(), moveBy(), setX(), setY() and setZ(). A canvas item can be set in motion, `animated', using setAnimated() and given a velocity in the x and y directions with setXVelocity() and setYVelocity() -- the same effect can be achieved by calling setVelocity(). Use the collidesWith() function to see if the canvas item will collide on the next advance(1) and use collisions() to see what collisions have occurred.

Use QtCanvasSprite or your own subclass of QtCanvasSprite to create canvas items which are animated, i.e. which change over time.

The size of a canvas item is given by boundingRect(). Use boundingRectAdvanced() to see what the size of the canvas item will be after the next advance(1) call.

The rtti() function is used for identifying subclasses of QtCanvasItem. The canvas() function returns a pointer to the canvas which contains the canvas item.

QtCanvasItem provides the show() and isVisible() functions like those in QWidget.

QtCanvasItem also provides the setEnabled(), setActive() and setSelected() functions; these functions set the relevant boolean and cause a repaint but the boolean values they set are not used in QtCanvasItem itself. You can make use of these booleans in your subclasses.

By default, canvas items have no velocity, no size, and are not in motion. The subclasses provided in Qt do not change these defaults except where noted.


Member Type Documentation

enum QtCanvasItem::RttiValues

This enum is used to name the different types of canvas item.

ConstantValueDescription
QtCanvasItem::Rtti_Item0Canvas item abstract base class
QtCanvasItem::Rtti_Ellipse6 
QtCanvasItem::Rtti_Line7 
QtCanvasItem::Rtti_Polygon4 
QtCanvasItem::Rtti_PolygonalItem2 
QtCanvasItem::Rtti_Rectangle5 
QtCanvasItem::Rtti_Spline8 
QtCanvasItem::Rtti_Sprite1 
QtCanvasItem::Rtti_Text3 

Member Function Documentation

QtCanvasItem::QtCanvasItem ( QtCanvas * canvas )

Constructs a QtCanvasItem on canvas canvas.

See also setCanvas().

QtCanvasItem::~QtCanvasItem ()   [virtual]

Destroys the QtCanvasItem and removes it from its canvas.

void QtCanvasItem::advance ( int phase )   [virtual]

The default implementation moves the canvas item, if it is animated(), by the preset velocity if phase is 1, and does nothing if phase is 0.

Note that if you reimplement this function, the reimplementation must not change the canvas in any way, for example it must not add or remove items.

See also QtCanvas::advance() and setVelocity().

bool QtCanvasItem::animated () const

Returns true if the canvas item is in motion; otherwise returns false.

See also setVelocity() and setAnimated().

QRect QtCanvasItem::boundingRect () const   [pure virtual]

Returns the bounding rectangle in pixels that the canvas item covers.

See also boundingRectAdvanced().

QRect QtCanvasItem::boundingRectAdvanced () const   [virtual]

Returns the bounding rectangle of pixels that the canvas item will cover after advance(1) is called.

See also boundingRect().

QtCanvas * QtCanvasItem::canvas () const

Returns the canvas containing the canvas item.

See also setCanvas().

bool QtCanvasItem::collidesWith ( const QtCanvasItem * other ) const   [pure virtual]

Returns true if the canvas item will collide with the other item after they have moved by their current velocities; otherwise returns false.

See also collisions().

QtCanvasItemList QtCanvasItem::collisions ( bool exact ) const

Returns the list of canvas items that this canvas item has collided with.

A collision is generally defined as occurring when the pixels of one item draw on the pixels of another item, but not all subclasses are so precise. Also, since pixel-wise collision detection can be slow, this function works in either exact or inexact mode, according to the exact parameter.

If exact is true, the canvas items returned have been accurately tested for collision with the canvas item.

If exact is false, the canvas items returned are near the canvas item. You can test the canvas items returned using collidesWith() if any are interesting collision candidates. By using this approach, you can ignore some canvas items for which collisions are not relevant.

The returned list is a list of QtCanvasItems, but often you will need to cast the items to their subclass types. The safe way to do this is to use rtti() before casting. This provides some of the functionality of the standard C++ dynamic cast operation even on compilers where dynamic casts are not available.

Note that a canvas item may be `on' a canvas, e.g. it was created with the canvas as parameter, even though its coordinates place it beyond the edge of the canvas's area. Collision detection only works for canvas items which are wholly or partly within the canvas's area.

Note that if items have a velocity (see setVelocity()), then collision testing is done based on where the item will be when it moves, not its current location. For example, a "ball" item doesn't need to actually embed into a "wall" item before a collision is detected. For items without velocity, plain intersection is used.

void QtCanvasItem::draw ( QPainter & painter )   [pure virtual]

This abstract virtual function draws the canvas item using painter.

void QtCanvasItem::hide ()

Shorthand for setVisible(false).

bool QtCanvasItem::isActive () const

Returns true if the QtCanvasItem is active; otherwise returns false.

bool QtCanvasItem::isEnabled () const

Returns true if the QtCanvasItem is enabled; otherwise returns false.

bool QtCanvasItem::isSelected () const

Returns true if the canvas item is selected; otherwise returns false.

bool QtCanvasItem::isVisible () const

Returns true if the canvas item is visible; otherwise returns false.

Note that in this context true does not mean that the canvas item is currently in a view, merely that if a view is showing the area where the canvas item is positioned, and the item is not obscured by items with higher z values, and the view is not obscured by overlaying windows, it would be visible.

See also setVisible() and z().

void QtCanvasItem::move ( double x, double y )

Moves the canvas item to the absolute position (x, y).

void QtCanvasItem::moveBy ( double dx, double dy )   [virtual]

Moves the canvas item relative to its current position by (dx, dy).

int QtCanvasItem::rtti () const   [virtual]

Returns 0 (QtCanvasItem::Rtti_Item).

Make your derived classes return their own values for rtti(), so that you can distinguish between objects returned by QtCanvas::at(). You should use values greater than 1000 to allow for extensions to this class.

Overuse of this functionality can damage its extensibility. For example, once you have identified a base class of a QtCanvasItem found by QtCanvas::at(), cast it to that type and call meaningful methods rather than acting upon the object based on its rtti value.

For example:

    QtCanvasItem* item;
    // Find an item, e.g. with QtCanvasItem::collisions().
    ...
    if (item->rtti() == MySprite::RTTI) {
        MySprite* s = (MySprite*)item;
        if (s->isDamagable()) s->loseHitPoints(1000);
        if (s->isHot()) myself->loseHitPoints(1000);
        ...
    }

void QtCanvasItem::setActive ( bool yes )   [virtual]

Sets the active flag of the item to yes. If this changes the item's active state the item will be redrawn when QtCanvas::update() is next called.

The QtCanvas, QtCanvasItem and the Qt-supplied QtCanvasItem subclasses do not make use of this value. The setActive() function is supplied because many applications need it, but it is up to you how you use the isActive() value.

See also active().

void QtCanvasItem::setAnimated ( bool y )   [virtual]

Sets the canvas item to be in motion if y is true, or not if y is false. The speed and direction of the motion is set with setVelocity(), or with setXVelocity() and setYVelocity().

See also animated(), advance(), and QtCanvas::advance().

void QtCanvasItem::setCanvas ( QtCanvas * c )   [virtual]

Sets the QtCanvas upon which the canvas item is to be drawn to c.

See also canvas().

void QtCanvasItem::setEnabled ( bool yes )   [virtual]

Sets the enabled flag of the item to yes. If this changes the item's enabled state the item will be redrawn when QtCanvas::update() is next called.

The QtCanvas, QtCanvasItem and the Qt-supplied QtCanvasItem subclasses do not make use of this value. The setEnabled() function is supplied because many applications need it, but it is up to you how you use the isEnabled() value.

See also enabled().

void QtCanvasItem::setSelected ( bool yes )   [virtual]

Sets the selected flag of the item to yes. If this changes the item's selected state the item will be redrawn when QtCanvas::update() is next called.

The QtCanvas, QtCanvasItem and the Qt-supplied QtCanvasItem subclasses do not make use of this value. The setSelected() function is supplied because many applications need it, but it is up to you how you use the isSelected() value.

See also selected().

void QtCanvasItem::setVelocity ( double vx, double vy )   [virtual]

Sets the canvas item to be in motion, moving by vx and vy pixels in the horizontal and vertical directions respectively.

See also advance(), setXVelocity(), and setYVelocity().

void QtCanvasItem::setVisible ( bool yes )   [virtual]

Makes the canvas item visible if yes is true, or invisible if yes is false. The change takes effect when QtCanvas::update() is next called.

See also visible().

void QtCanvasItem::setX ( double x )

Moves the canvas item so that its x-position is x.

See also x() and move().

void QtCanvasItem::setXVelocity ( double vx )

Sets the horizontal component of the canvas item's velocity to vx.

See also xVelocity(), setYVelocity(), and setVelocity().

void QtCanvasItem::setY ( double y )

Moves the canvas item so that its y-position is y.

See also y() and move().

void QtCanvasItem::setYVelocity ( double vy )

Sets the vertical component of the canvas item's velocity to vy.

See also yVelocity(), setXVelocity(), and setVelocity().

void QtCanvasItem::setZ ( double z )

Sets the z index of the canvas item to z. Higher-z items obscure (are in front of) lower-z items.

See also z() and move().

void QtCanvasItem::show ()

Shorthand for setVisible(true).

void QtCanvasItem::update ()   [protected]

Call this function to repaint the canvas's changed chunks.

double QtCanvasItem::x () const

Returns the horizontal position of the canvas item. Note that subclasses often have an origin other than the top-left corner.

See also setX().

double QtCanvasItem::xVelocity () const

Returns the horizontal velocity component of the canvas item.

See also setXVelocity().

double QtCanvasItem::y () const

Returns the vertical position of the canvas item. Note that subclasses often have an origin other than the top-left corner.

See also setY().

double QtCanvasItem::yVelocity () const

Returns the vertical velocity component of the canvas item.

See also setYVelocity().

double QtCanvasItem::z () const

Returns the z index of the canvas item, which is used for visual order: higher-z items obscure (are in front of) lower-z items.

See also setZ().


Copyright © 2005 Trolltech Trademarks