QCanvas

Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index Return to Main Contents
 

NAME

QCanvas - 2D area that can contain QCanvasItem objects  

SYNOPSIS

#include <qcanvas.h>

Inherits QObject.

 

Public Members


QCanvas ( QObject * parent = 0, const char * name = 0 )

QCanvas ( int w, int h )

QCanvas ( QPixmap p, int h, int v, int tilewidth, int tileheight )

virtual ~QCanvas ()

virtual void setTiles ( QPixmap p, int h, int v, int tilewidth, int tileheight )

virtual void setBackgroundPixmap ( const QPixmap & p )

QPixmap backgroundPixmap () const

virtual void setBackgroundColor ( const QColor & c )

QColor backgroundColor () const

virtual void setTile ( int x, int y, int tilenum )

int tile ( int x, int y ) const

int tilesHorizontally () const

int tilesVertically () const

int tileWidth () const

int tileHeight () const

virtual void resize ( int w, int h )

int width () const

int height () const

QSize size () const

QRect rect () const

bool onCanvas ( int x, int y ) const

bool onCanvas ( const QPoint & p ) const

bool validChunk ( int x, int y ) const

bool validChunk ( const QPoint & p ) const

int chunkSize () const

virtual void retune ( int chunksze, int mxclusters = 100 )

virtual void setAllChanged ()

virtual void setChanged ( const QRect & area )

virtual void setUnchanged ( const QRect & area )

QCanvasItemList allItems ()

QCanvasItemList collisions ( const QPoint & p ) const

QCanvasItemList collisions ( const QRect & r ) const

QCanvasItemList collisions ( const QPointArray & chunklist, const QCanvasItem * item, bool exact ) const

void drawArea ( const QRect & clip, QPainter * painter, bool dbuf = FALSE )

virtual void setAdvancePeriod ( int ms )

virtual void setUpdatePeriod ( int ms )

virtual void setDoubleBuffering ( bool y )
 

Public Slots


virtual void advance ()

virtual void update ()
 

Signals


void resized ()
 

Protected Members


virtual void drawBackground ( QPainter & painter, const QRect & clip )

virtual void drawForeground ( QPainter & painter, const QRect & clip )
 

DESCRIPTION

The QCanvas class provides a 2D area that can contain QCanvasItem objects.

The QCanvas class manages its 2D graphic area and all the canvas items the area contains. The canvas has no visual appearance of its own. Instead, it is displayed on screen using a QCanvasView. Multiple QCanvasView widgets may be associated with a canvas to provide multiple views of the same canvas.

The canvas is optimized for large numbers of items, particularly where only a small percentage of the items change at any one time. If the entire display changes very frequently, you should consider using your own custom QScrollView subclass.

Qt provides a rich set of canvas item classes, e.g. QCanvasEllipse, QCanvasLine, QCanvasPolygon, QCanvasPolygonalItem, QCanvasRectangle, QCanvasSpline, QCanvasSprite and QCanvasText. You can subclass to create your own canvas items; QCanvasPolygonalItem is the most common base class used for this purpose.

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. (By default the canvas is white and so are canvas items, so if nothing appears try changing colors.)

If you created the canvas without passing a width and height to the constructor you must also call resize().

Although a canvas may appear to be similar to a widget with child widgets, there are several notable differences:

Canvas items are usually much faster to manipulate and redraw than child widgets, with the speed advantage becoming especially great when there are many canvas items and non-rectangular items. In most situations canvas items are also a lot more memory efficient than child widgets.
It's easy to detect overlapping items (collision detection).
The canvas can be larger than a widget. A million-by-million canvas is perfectly possible. At such a size a widget might be very inefficient, and some window systems might not support it at all, whereas QCanvas scales well. Even with a billion pixels and a million items, finding a particular canvas item, detecting collisions, etc., is still fast (though the memory consumption may be prohibitive at such extremes).
Two or more QCanvasView objects can view the same canvas.
An arbitrary transformation matrix can be set on each QCanvasView which makes it easy to zoom, rotate or shear the viewed canvas.
Widgets provide a lot more functionality, such as input (QKeyEvent, QMouseEvent etc.) and layout management (QGridLayout etc.).

A canvas consists of a background, a number of canvas items organized by x, y and z coordinates, and a foreground. A canvas item's z coordinate can be treated as a layer number -- canvas items with a higher z coordinate appear in front of canvas items with a lower z coordinate.

The background is white by default, but can be set to a different color using setBackgroundColor(), or to a repeated pixmap using setBackgroundPixmap() or to a mosaic of smaller pixmaps using setTiles(). Individual tiles can be set with setTile(). There are corresponding get functions, e.g. backgroundColor() and backgroundPixmap().

Note that QCanvas does not inherit from QWidget, even though it has some functions which provide the same functionality as those in QWidget. One of these is setBackgroundPixmap(); some others are resize(), size(), width() and height(). QCanvasView is the widget used to display a canvas on the screen.

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 QCanvasItem::setCanvas().

Canvas items are movable (and in the case of QCanvasSprites, animated) objects that inherit QCanvasItem. Each canvas item has a position on the canvas (x, y coordinates) and a height (z coordinate), all of which are held as floating-point numbers. Moving canvas items also have x and y velocities. It's possible for a canvas item to be outside the canvas (for example QCanvasItem::x() is greater than width()). When a canvas item is off the canvas, onCanvas() returns FALSE and the canvas disregards the item. (Canvas items off the canvas do not slow down any of the common operations on the canvas.)

Canvas items can be moved with QCanvasItem::move(). The advance() function moves all QCanvasItem::animated() canvas items and setAdvancePeriod() makes QCanvas move them automatically on a periodic basis. In the context of the QCanvas classes, to `animate' a canvas item is to set it in motion, i.e. using QCanvasItem::setVelocity(). Animation of a canvas item itself, i.e. items which