QCanvas(3qt)

πŸ“¦ version 3.3.8b, in fedora 43, fedora 44

πŸ’– QCanvas πŸ’–

the 2D area that can contain QCanvasItem objects β€” and honestly? she's that girl. this is from the Qt manual, section 3qt, and we're about to make it make sense fr fr πŸ¦‹


✨ NAME ✨

QCanvas β€” a 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 ✨

okay so. The QCanvas class provides a 2D area that can contain QCanvasItem objects. that's the whole vibe. πŸ’–

The QCanvas class manages its 2D graphic area and all the canvas items that area contains. And here's the thing bestie β€” the canvas has no visual appearance of its own. like, none. zero. she's behind the scenes. Instead, she gets displayed on screen using a QCanvasView. And you can hook up multiple QCanvasView widgets to one canvas, so you get multiple views of the exact same canvas. iconic tbh πŸ‘‘

The canvas is optimized for large numbers of items, especially when only a small percentage of the items change at any one time. But if your entire display changes very frequently? then ngl you should consider using your own custom QScrollView subclass instead. different tool, different job πŸ’…

Qt ships a rich set of canvas item classes, e.g. QCanvasEllipse, QCanvasLine, QCanvasPolygon, QCanvasPolygonalItem, QCanvasRectangle, QCanvasSpline, QCanvasSprite and QCanvasText. You can subclass to make your own canvas items too β€” and QCanvasPolygonalItem is the most common base class people reach for when they do. πŸŽ€

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 β€” yeah β€” try changing colors. 😭 we've all been there.)

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

So a canvas might look like a widget with child widgets, but lowkey there are several notable differences:

  • Canvas items are usually much faster to manipulate and redraw than child widgets, and that speed advantage gets 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. slay πŸ’š
  • 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. stack 'em like a deck of cards fr fr πŸƒ

The background is white by default, but you can set it 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(). And 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. don't get it twisted 🫢

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. so she's not even mad about it πŸ’…)

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 change over time β€” is enabled by calling QCanvasSprite::setFrameAnimation(), or more generally by subclassing and reimplementing QCanvasItem::advance(). To detect collisions use one of the QCanvasItem::collisions()` functions. πŸ’₯

The changed parts of the canvas are redrawn (if they are visible in a canvas view) whenever update() is called. You can either call update() manually after having changed the contents of the canvas, or force periodic updates using setUpdatePeriod(). If you have moving objects on the canvas, you must call advance() every time the objects should move one step further. Periodic calls to advance() can be forced using setAdvancePeriod(). The advance() function will call QCanvasItem::advance() on every item that is animated and trigger an update of the affected areas afterwards. (A canvas item that is `animated' is simply a canvas item that is in motion.) πŸƒβ™€οΈ

QCanvas organizes its canvas items into chunks; these are areas on the canvas that are used to speed up most operations. Many operations start by eliminating most chunks (i.e. those which haven't changed) and then process only the canvas items that are in the few interesting (i.e. changed) chunks. A valid chunk, validChunk(), is one which is on the canvas.

The chunk size is a key factor to QCanvas's speed: if there are too many chunks, the speed benefit of grouping canvas items into chunks is reduced. If the chunks are too large, it takes too long to process each one. The QCanvas constructor tries to pick a suitable size, but you can call retune() to change it at any time. The chunkSize() function returns the current chunk size. The canvas items always make sure they're in the right chunks; all you need to make sure of is that the canvas uses the right chunk size. A good rule of thumb is that the size should be a bit smaller than the average canvas item size. If you have moving objects, the chunk size should be a bit smaller than the average size of the moving items. πŸ“

The foreground is normally nothing, but if you reimplement drawForeground(), you can draw things in front of all the canvas items. 🌈

Areas can be set as changed with setChanged() and set unchanged with setUnchanged(). The entire canvas can be set as changed with setAllChanged(). A list of all the items on the canvas is returned by allItems(). πŸ“‹

An area can be copied (painted) to a QPainter with drawArea().

If the canvas is resized it emits the resized() signal. πŸ“£

The examples/canvas application and the 2D graphics page of the examples/demo application demonstrate many of QCanvas's facilities. go look, they're so good 🫢

See also QCanvasView, QCanvasItem, Abstract Widget Classes, Graphics Classes, and Image Processing Classes.


πŸ‘‘ MEMBER FUNCTION DOCUMENTATION πŸ‘‘

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

Create a QCanvas with no size. parent and name are passed to the QObject superclass.

Warning: You must call resize() at some time after creation to be able to use the canvas. no size = no canvas bestie, she needs her dimensions 😭

QCanvas::QCanvas ( int w, int h )

Constructs a QCanvas that is w pixels wide and h pixels high. clean. simple. she's ready. ✨

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 taken from pixmap p. 🧩

The pixmap p is a list of tiles, arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.

(there's a lil diagram right here in the original β€” it's showing you the tile layout, tile 0 up top-left, tile 1 to its right, and so on. imagine a lil grid, she's very cute πŸŽ€)

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 often as necessary to cover the area. If it is smaller, tiles to the right and bottom will not be visible. πŸ¦‹

See also setTiles().

QCanvas::~QCanvas () [virtual]

Destroys the canvas and all the canvas's canvas items. everything goes. take a moment. πŸ’€


to be continued bestie… more member functions incoming πŸ’œ

  • void QCanvas::advance () [virtual slot] β€” moves every QCanvasItem::animated() item on the canvas, then refreshes all the changes across all views. (An "animated" item is one that's in motion β€” peep setVelocity() for that.)

So here's the tea on how advance() actually works, because it's a whole two-phase situation. In phase 0, QCanvasItem::advance() gets called on every QCanvasItem::animated() item with parameter 0. Then all those same items get called again with parameter 1. In phase 0 your items should not move β€” they're just supposed to look around at other items on the canvas for anything needing special handling, like collisions between items. In phase 1, all the items should change position, ignoring every other item on the canvas. This two-phase thing exists so "fairness" can be considered, even though ngl none of the QCanvasItem subclasses shipped with Qt do anything interesting in phase 0.

You can make the canvas call this function on a schedule with setAdvancePeriod().

See also update().


  • QCanvasItemList QCanvas::allItems () β€” hands you a list of every single item on the canvas. the whole roster. πŸ’–
  • QColor QCanvas::backgroundColor () const β€” returns the color that setBackgroundColor() set. By default it's white.

Just so we're clear, this is not a reimplementation of QWidget::backgroundColor() (because QCanvas is not a subclass of QWidget), but every QCanvasView looking at this canvas will set its background to this color. πŸŽ€

See also setBackgroundColor() and backgroundPixmap().

  • QPixmap QCanvas::backgroundPixmap () const β€” returns the pixmap that setBackgroundPixmap() set. By default it's a null pixmap.

See also setBackgroundPixmap() and backgroundColor().

  • int QCanvas::chunkSize () const β€” returns the chunk size of the canvas.

See also retune().

  • QCanvasItemList QCanvas::collisions ( const QPoint & p ) const β€” gives you a list of canvas items that collide with the point p. The list is ordered by z coordinates, from the highest z coordinate (front-most item) all the way down to the lowest z coordinate (rear-most item). front to back, bestie. ✨
  • QCanvasItemList QCanvas::collisions ( const QRect & r ) const β€” this is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a list of items which collide with the rectangle r. The list is ordered by z coordinates, from highest z coordinate (front-most item) to lowest z coordinate (rear-most item).

  • QCanvasItemList QCanvas::collisions ( const QPointArray & chunklist, const QCanvasItem * item, bool exact ) const β€” another overloaded member function, provided for convenience. Behaves essentially like the above function.

Returns a list of canvas items which intersect with the chunks listed in chunklist, excluding item. If exact is TRUE, only the ones that actually collide with item get returned; otherwise canvas items are included just for being in the chunks. lowkey generous of it.

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

  • void QCanvas::drawArea ( const QRect & clip, QPainter * painter, bool dbuf = FALSE ) β€” paints all canvas items that are in the area clip onto painter, using double-buffering if dbuf is TRUE.

e.g. to print the canvas to a printer:

QPrinter pr;
if ( pr.setup() ) {
    QPainter p(&pr);
    canvas.drawArea( canvas.rect(), &p );
}

Example: canvas/canvas.cpp.

  • void QCanvas::drawBackground ( QPainter & painter, const QRect & clip ) [virtual protected] β€” this virtual function gets called for all updates of the canvas. It renders any background graphics using the painter painter, in the area clip. If the canvas has a background pixmap or a tiled background, that graphic is what gets used; otherwise the canvas is cleared using the background color.

If the graphics for an area change, you have to explicitly call setChanged(const QRect&) for the result to actually show up when update() is next called. no silent updates around here. πŸ’…

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 canvas. It renders any foreground graphics using the painter painter, in the area clip.

Same rule as above: 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.

The default is to draw nothing. iconic minimalism honestly. πŸ¦‹

  • int QCanvas::height () const β€” returns the height of the canvas, in pixels.

Example: canvas/canvas.cpp.

  • bool QCanvas::onCanvas ( int x, int y ) const β€” returns TRUE if the pixel position (x, y) is on the canvas; otherwise returns FALSE.

See also validChunk().

  • bool QCanvas::onCanvas ( const QPoint & p ) const β€” overloaded member function, provided for convenience. Behaves essentially like the above function.

Returns TRUE if the pixel position p is on the canvas; otherwise returns FALSE.

See also validChunk().

  • QRect QCanvas::rect () const β€” returns a rectangle the size of the canvas.
  • void QCanvas::resize ( int w, int h ) [virtual] β€” changes the size of the canvas to have a width of w and a height of h. Heads up: this is a slow operation. 😭

Examples:

  • void QCanvas::resized () [signal] β€” this signal is emitted whenever the canvas is resized. Each QCanvasView connects to this signal to keep the scrollview's size correct.
  • void QCanvas::retune ( int chunksze, int mxclusters = 100 ) [virtual] β€” changes the efficiency tuning parameters to mxclusters clusters, each of size chunksze. This is a slow operation if there are a lot of objects on the canvas.

The canvas is divided into chunks, which are rectangular areas chunksze wide by chunksze high. Use a chunk size that's roughly the average size of your canvas items. Pick a chunk size that's too small and you increase the amount of calculation needed when drawing, because each change will affect many chunks. Pick one that's too large and the amount of drawing needed goes up, because for each change a lot of drawing is required β€” there'll be many (unchanged) canvas items sitting in the same chunk as the changed canvas items.

Internally, a canvas uses a low-resolution "chunk matrix" to keep track of all the items on the canvas. A 64x64 chunk matrix is the default for a 1024x1024 pixel canvas, where each chunk collects canvas items in a 16x16 pixel square. This default is also affected by setTiles(). You can tune that default with this function. For example, if you have a very large canvas and want to trade off speed for memory, you might set the chunk size to 32 or 64.

The mxclusters argument is the number of rectangular groups of chunks that will be separately drawn. If the canvas has a large number of small, dispersed items, this should be about that number. Our testing suggests that a large number of clusters is almost always best. trust the testing bestie. πŸ”₯

  • void QCanvas::setAdvancePeriod ( int ms ) [virtual] β€” sets the canvas to call advance() every ms milliseconds. Any previous setting from setAdvancePeriod() or setUpdatePeriod() is overridden.

If ms is less than 0, advancing will be stopped.

Example: canvas/main.cpp.

  • void QCanvas::setAllChanged () [virtual] β€” marks the whole canvas as changed. All views of the canvas will be entirely redrawn when update() is called next.
  • 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 the pixmap p repeated as necessary to cover the entire canvas.

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

  • void QCanvas::setChanged ( const QRect & area ) [virtual] β€” marks area as changed. This area will be redrawn in all views showing it when update() is called next.
  • void QCanvas::setDoubleBuffering ( bool y ) [virtual] β€” if y is TRUE (which is the default) double-buffering is switched on; otherwise double-buffering is switched off.

Turning off double-buffering makes the redrawn areas flicker a little, and also gives a (usually small) performance improvement. tradeoffs, we love them. ⭐

Example: canvas/canvas.cpp.

  • 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 come from the pixmap set by setTiles() and are arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.

<center>.nf </center>

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 is a list of tiles, arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.

<center>.nf </center>

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

The width and height of p must be a multiple of tilewidth and tileheight. If they're not, the function will do nothing. 🫢

If you want to unset any tiling that's set, just pass in a null pixmap and 0 for h, v, tilewidth, and tileheight.

  • void QCanvas::setUnchanged ( const QRect & area ) [virtual] β€” marks area as unchanged. The area will not be redrawn in the views for the next update(), unless it's marked or changed again before the next call to update().
  • void QCanvas::setUpdatePeriod ( int ms ) [virtual] β€” sets the canvas to call update() every ms milliseconds. Any previous setting from setAdvancePeriod() or setUpdatePeriod() is overridden.

If ms is less than 0, automatic updating will be stopped.

  • QSize QCanvas::size () const β€” returns the size of the canvas, in pixels.
  • int QCanvas::tile ( int x, int y ) const β€” returns the tile at position (x, y). Initially, all tiles are 0.

The parameters must be within range, i.e. 0 < x < tilesHorizontally() and 0 < y < tilesVertically().

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] β€” repaints changed areas in all views of the canvas. the glow-up pass, fr fr. πŸ’œ

See also advance().

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

okay bestie, this one is literally the chunk coordinate vibe check πŸ’…. It returns TRUE if the chunk position (x, y) is on the canvas; otherwise returns FALSE. no cap.

See also onCanvas().


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

this is an overloaded member function, provided for convenience. It behaves essentially like the above function. (same energy, different arg ✨)

Returns TRUE if the chunk position p is on the canvas; otherwise returns FALSE.

See also onCanvas().


int QCanvas::width () const

Returns the width of the canvas, in pixels. literally just the horizontal size, bestie. πŸ“

Example: canvas/canvas.cpp.


SEE ALSO


Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.


AUTHOR

Generated automatically from the source code. (the bots did this one, iconic πŸ€–)


BUGS

If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. 🫢

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qcanvas.3qt) and the Qt version (3.3.8).


and finally, the footer tea β˜•: this page came from Trolltech AS, dated 2 February 2007, for QCanvas(3qt). That's the whole vibe. Stay slay, keep your chunks valid, and don't let your canvas resize without a resized() signal. bye bestie πŸ’–

πŸ“¦ where this page comes from