Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Classes  ·  Annotées  ·  Hiérarchie  ·  Fonctions  ·  Structure  · 

QCanvas Class Reference
[ canvas module ]


The QCanvas class is a 2D graphic area upon which QCanvasItem objects exist. More...

#include <qcanvas.h>

Inherits QObject.

List of all member functions.

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 tiles, 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 width, int height ) 
  • int width () const
  • int height () const
  • QSize size () 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 chunksize, int maxclusters=100 ) 
  • bool sameChunk ( int x1, int y1, int x2, int y2 ) const (internal)
  • virtual void setChangedChunk ( int i, int j ) (internal)
  • virtual void setChangedChunkContaining ( int x, int y ) (internal)
  • virtual void setAllChanged () 
  • virtual void setChanged ( const QRect & inarea ) 
  • void addItemToChunk ( QCanvasItem *, int i, int j ) (internal)
  • void removeItemFromChunk ( QCanvasItem *, int i, int j ) (internal)
  • void addItemToChunkContaining ( QCanvasItem *, int x, int y ) (internal)
  • void removeItemFromChunkContaining ( QCanvasItem *, int x, int y ) (internal)
  • QCanvasItemList allItems () 
  • QCanvasItemList collisions ( const QPoint & ) const
  • QCanvasItemList collisions ( const QRect & ) const
  • QCanvasItemList collisions ( const QPointArray & pa, const QCanvasItem * item, bool exact ) const
  • virtual void addView ( QCanvasView * ) (internal)
  • virtual void removeView ( QCanvasView * ) (internal)
  • void drawArea ( const QRect &, QPainter * p=0, bool double_buffer=TRUE ) (internal)
  • virtual void addItem ( QCanvasItem * ) (internal)
  • virtual void addAnimation ( QCanvasItem * ) (internal)
  • virtual void removeItem ( QCanvasItem * ) (internal)
  • virtual void removeAnimation ( QCanvasItem * ) (internal)
  • virtual void setAdvancePeriod ( int ms ) 
  • virtual void setUpdatePeriod ( int ms ) 
  • virtual void setDoubleBuffering ( bool y ) 

Public Slots

  • virtual void advance () 
  • virtual void update () 

Signals

Protected Members

  • virtual void drawBackground ( QPainter &, const QRect & area ) 
  • virtual void drawForeground ( QPainter &, const QRect & area ) 

Detailed Description

The QCanvas class is a 2D graphic area upon which QCanvasItem objects exist.

A QCanvas contains any number of QCanvasItem subclassed objects and has any number of QCanvasView widgets observing some part of the canvas.

A canvas containing many items is different to a widgets containing many subwidgets in the following ways:

  • Items are drawn much faster than widgets, especially when non-rectangular.
  • Items use less memory than widgets.
  • You can do efficient item-to-item hit tests ("collision detection") with items in a canvas.
  • Finding items in an area is efficient.
  • You can have multiple views of a canvas.

Widgets of course offer richer functionality, such as hierarchies, events, layout, etc.

Drawing

A canvas has a solid background and a foreground. By default, the canvas will have a white background, which can be changed with setBackgroundColor(). If you want an image, use setBackgroundPixmap(). A third option is to use tiles, where the canvas background is a matrix of small images all the same size, each chosen from a defined larger pixmap. See setTiles().

On top of the background are objects of QCanvasItems subclasses. Each item has a Z-height (see QCanvasItem::z()), with the lower-Z items on the background and higher-Z items on top of them.

Above everything in the canvas is the foreground, as defined by the drawForeground() function. By default this function draws nothing.

Changes to the items on the canvas are refreshed to the views whenever update() is called, including creation of new items, movement of item, change of shape, change of visibility, and destruction.

Note that like QWidgets, QCanvasItems are always hidden when they are created, so you must show() them some time after creating them if you wish them to be visible.

Animation

QCanvas has some built-in animation features. If you call QCanvasItem::setVelocity() on an item, it will move forward whenever advance() is call. The advance() function also calls update(), so you only need to call one or the other. If no items have a velocity, then advance() is the same as update().

You can have advance() or update() called automatically with setAdvancePeriod() or setUpdatePeriod() respectively.

Collision Detection

Items on the canvas can be tested for collisions with these functions, each of which returns a list of items which match the hit, sorted from top to bottom (ie. by decreasing QCanvasItem::z() value).

  • collisions(QPoint) - items which will collide with a point.
  • collisions(QRect) - items which will collide with a rectangle.

You can also test for item-to-item collisions with QCanvasItem::collisions().


Member Function Documentation

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

Create a QCanvas with no size. You will want to call resize(int,int) at some time after creation.

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

Constructs a QCanvas which will be composed of h tiles horizontally and v tiles vertically. Each tile will be an image tilewidth by tileheight pixels from pixmap p.

The pixmap p is a list of tiles, arranged left to right, top to bottom, with tile 0 in the top-left corner, tile 1 next to the right, and so on.

The QCanvas is initially sized to show exactly the given number of tiles horizontally and vertically. If it is resized to be larger, the entire matrix of tiles will be repeated as much as necessary to cover the area. If it is smaller, tiles to the right and bottom will not be visible.

QCanvas::QCanvas ( int w, int h )

Constructs a QCanvas with that is w pixels wide and h pixels high.

QCanvas::~QCanvas () [virtual]

Destructs the canvas. Does also destroy all items on the canvas.

void QCanvas::advance () [virtual slot]

Advances the animation of items on the canvas and refreshes all changes to all views of the canvas.

The advance is done in two phases. In phase 0, the QCanvasItem:advance() function of each animated item is called with paramater 0. Then all items are called again, with parameter 1. In phase 0, the items should not change position, merely examine other items on the canvas for which special processing is required, such as collisions between items. In phase 1, all items should change positions, ignoring any other items on the canvas. This two-phase approach allows for considerations of "fairness", though no QCanvasItem subclasses supplied with Qt do anything interesting in phase 0.

The canvas can be configured to call this function periodically with setAdvancePeriod().

See also update().

QCanvasItemList QCanvas::allItems ()

Returns a list of all items in the canvas.

QColor QCanvas::backgroundColor () const

Returns the color set by setBackgroundColor(). By default, this is white.

Note that this function is not a reimplementation of QWidget::backgroundColor() (QCanvas is not a subclass of QWidget), but all QCanvasViews that are viewing the canvas will set their backgrounds to this

See also setBackgroundColor() and backgroundPixmap().

QPixmap QCanvas::backgroundPixmap () const

Returns the pixmap set by setBackgroundPixmap(). By default, this is a null pixmap.

See also setBackgroundPixmap() and backgroundColor().

int QCanvas::chunkSize () const

Returns the chunk size of the canvas as set at construction.

See also retune().

QCanvasItemList QCanvas::collisions ( const QPoint & p ) const

Returns a list of items which intersect with the point p, sorted from shallowest to deepest.

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

Returns a list of items which intersect with the chunks listed in chunklist, excluding item. If exact is TRUE, only only those which actually QCanvasItem::collidesWith() item are returned, otherwise items are included just for being in the chunks.

This is a utility function mainly used to implement the simpler QCanvasItem::collisions() function.

QCanvasItemList QCanvas::collisions ( const QRect & r ) const

Returns a list of items which intersect with the rectangle r, sorted from shallowest to deepest.

void QCanvas::drawBackground ( QPainter & painter, const QRect & clip ) [virtual protected]

This virtual function is called for all updates of the QCanvas. It renders any background graphics. If the canvas has a background pixmap or a tiled background, that graphic is used, otherwise the canvas is cleared in the background color.

If the graphics for an area change, you must explicitly call setChanged(const QRect&) for the result to be visible when update() is next called.

See also setBackgroundColor(), setBackgroundPixmap() and setTiles().

void QCanvas::drawForeground ( QPainter & painter, const QRect & clip ) [virtual protected]

This virtual function is called for all updates of the QCanvas. It renders any foreground graphics.

The same warnings regarding change apply to this method as for drawBackground().

The default is to draw nothing.

int QCanvas::height () const

Returns the height of the canvas, in pixels.

bool QCanvas::onCanvas ( const QPoint & p ) const

Returns whether the pixel position p is on the canvas.

bool QCanvas::onCanvas ( int x, int y ) const

Returns whether the pixel position (x, y) is on the canvas.

void QCanvas::resize ( int w, int h ) [virtual]

Changes the size of the QCanvas. This is a slow operation.

void QCanvas::resized () [signal]

This signal is emitted whenever the canvas is resized. Each QCanvasView connects to this signal to keep the scrollview size correct.

void QCanvas::retune ( int chunksze, int mxclusters=100 ) [virtual]

Change the efficiency tuning parameters to mxclusters clusters, each of size chunksze (square). This is a slow operation if you have many objects on the canvas.

Internally, a canvas uses a low-resolution "chunk matrix" to keep track of all the items in the canvas. In Qt 2.2, the default for a 1024x1024 pixel canvas is to have a 64x64 chunk matrix, where each of those chunks collects items in a 16x16 pixel square.

This default is also affected by setTiles(). You can tune this default by using retune(), for example if you have a very large canvas and want to trade off speed for memory then you might set the chunk size to 32 or 64.

chunksze is the size of square chunk used to break up the QCanvas into area to be considered for redrawing. It should be about the average size of items in the QCanvas. Chunks too small increase the amount of calculation required when drawing. Chunks too large increase the amount of drawing that is needed.

mxclusters is the number of rectangular groups of chunks that will be separately drawn. If the QCanvas has a large number of small, dispersed items, this should be about that number. The more clusters the slower the redraw, but also the bigger clusters are the slower the redraw, so a balance is needed. Testing indicates that a large number of clusters is almost always best.

void QCanvas::setAdvancePeriod ( int ms ) [virtual]

Sets the canvas to call advance() every ms milliseconds. Any previous setting by setAdvancePeriod() or setUpdatePeriod() is cancelled.

void QCanvas::setAllChanged () [virtual]

Sets all views of the canvas to be entirely redrawn when update() is next called.

void QCanvas::setBackgroundColor ( const QColor & c ) [virtual]

Sets the solid background to be the color c.

See also backgroundColor(), setBackgroundPixmap() and setTiles().

void QCanvas::setBackgroundPixmap ( const QPixmap & p ) [virtual]

Sets the solid background to be p, repeated as necessary to cover the entire canvas.

See also backgroundPixmap(), setBackgroundColor() and setTiles().

void QCanvas::setChanged ( const QRect & area ) [virtual]

Sets all views of area to be entirely redrawn when update() is next called.

void QCanvas::setDoubleBuffering ( bool y ) [virtual]

Turns double-buffering on if y is TRUE, or off if it is FALSE. The default is to use double-buffering.

Turning off double-buffering casuses the redrawn areas to flicker a bit. This can help understand the the optimizations made by QCanvas and also gives a (usually small) performance improvement.

void QCanvas::setTile ( int x, int y, int tilenum ) [virtual]

Sets the tile at (x, y) to use tile number tilenum, which is an index into the tile pixmaps. The canvas will update appropriately when update() is next called.

The images are taken from the pixmap set by setTiles() and are arranged in the pixmap left to right, top to bottom, with tile 0 in the top-left corner, tile 1 next to the right, and so on.

See also tile() and setTiles().

void QCanvas::setTiles ( QPixmap p, int h, int v, int tilewidth, int tileheight ) [virtual]

Sets the QCanvas to be composed of h tiles horizontally and v tiles vertically. Each tile will be an image tilewidth by tileheight pixels from pixmap p.

The pixmap p contains the tiles arranged left to right, top to bottom, with tile 0 in the top-left corner, tile 1 to the right of tile 0, and so on.

If the QCanvas is larger than the matrix of tiles, the entire matrix is repeated as necessary to cover the area. If it is smaller, tiles to the right and bottom are not visible.

The width and height of p must be multiples of tilewidth and tileheight. If they are not, the action of this function is unspecified.

void QCanvas::setUpdatePeriod ( int ms ) [virtual]

Sets the canvas to call update() every ms milliseconds. Any previous setting by setAdvancePeriod() or setUpdatePeriod() is cancelled.

QSize QCanvas::size () const

Returns the size of the canvas, in pixels.

int QCanvas::tile ( int x, int y ) const

Returns the tile at (x, y). Initially, all tiles are 0.

Warning: The parameters must be within range.

See also setTile().

int QCanvas::tileHeight () const

Returns the height of each tile.

int QCanvas::tileWidth () const

Returns the width of each tile.

int QCanvas::tilesHorizontally () const

Returns the number of tiles horizontally.

int QCanvas::tilesVertically () const

Returns the number of tiles vertically.

void QCanvas::update () [virtual slot]

Refreshes all changes to all views of the canvas.

See also advance().

bool QCanvas::validChunk ( const QPoint & p ) const

Returns whether the chunk position p is on the canvas.

bool QCanvas::validChunk ( int x, int y ) const

Returns whether the chunk position (x, y) is on the canvas.

int QCanvas::width () const

Returns the width of the canvas, in pixels.

void QCanvas::addAnimation ( QCanvasItem * item ) [virtual]

For internal use only.

void QCanvas::addItem ( QCanvasItem * item ) [virtual]

For internal use only.

void QCanvas::addItemToChunk ( QCanvasItem * g, int x, int y )

For internal use only.

void QCanvas::addItemToChunkContaining ( QCanvasItem * g, int x, int y )

For internal use only.

void QCanvas::addView ( QCanvasView * view ) [virtual]

For internal use only.

void QCanvas::drawArea ( const QRect & inarea, QPainter * p=0, bool double_buffer=TRUE )

For internal use only.

void QCanvas::removeAnimation ( QCanvasItem * item ) [virtual]

For internal use only.

void QCanvas::removeItem ( QCanvasItem * item ) [virtual]

For internal use only.

void QCanvas::removeItemFromChunk ( QCanvasItem * g, int x, int y )

For internal use only.

void QCanvas::removeItemFromChunkContaining ( QCanvasItem * g, int x, int y )

For internal use only.

void QCanvas::removeView ( QCanvasView * view ) [virtual]

For internal use only.

bool QCanvas::sameChunk ( int x1, int y1, int x2, int y2 ) const

For internal use only.

void QCanvas::setChangedChunk ( int x, int y ) [virtual]

For internal use only.

void QCanvas::setChangedChunkContaining ( int x, int y ) [virtual]

For internal use only.


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2005 Trolltech, all rights reserved.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 103
  2. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 53
  3. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 82
  4. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 30
  5. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 11
Page suivante
  1. Linus Torvalds : le "C++ est un langage horrible", en justifiant le choix du C pour le système de gestion de version Git 100
  2. Comment prendre en compte l'utilisateur dans vos applications ? Pour un développeur, « 90 % des utilisateurs sont des idiots » 229
  3. Quel est LE livre que tout développeur doit lire absolument ? Celui qui vous a le plus marqué et inspiré 96
  4. Apple cède et s'engage à payer des droits à Nokia, le conflit des brevets entre les deux firmes s'achève 158
  5. Nokia porte à nouveau plainte contre Apple pour violation de sept nouveaux brevets 158
  6. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 102
  7. Quel est le code dont vous êtes le plus fier ? Pourquoi l'avez-vous écrit ? Et pourquoi vous a-t-il donné autant de satisfaction ? 83
Page suivante

Le Qt Developer Network au hasard

Logo

Combiner licence, à propos et fermer

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web