The Q3CanvasItem class provides an abstract graphic object on a Q3Canvas . 更多...
| 头: | #include <Q3CanvasItem> |
| 继承者: | Q3CanvasPolygonalItem , Q3CanvasSprite ,和 Q3CanvasText |
| enum | RttiValues { Rtti_Item, Rtti_Ellipse, Rtti_Line, Rtti_Polygon, ..., Rtti_Text } |
| Q3CanvasItem (Q3Canvas * canvas ) | |
| virtual | ~Q3CanvasItem () |
| virtual void | advance (int phase ) |
| bool | animated () const |
| virtual QRect | boundingRect () const = 0 |
| virtual QRect | boundingRectAdvanced () const |
| Q3Canvas * | canvas () const |
| virtual bool | collidesWith (const Q3CanvasItem * other ) const = 0 |
| Q3CanvasItemList | collisions (bool exact ) const |
| virtual void | draw (QPainter & painter ) = 0 |
| void | hide () |
| bool | isActive () const |
| bool | isEnabled () const |
| bool | isSelected () const |
| bool | isVisible () const |
| void | move (double x , double y ) |
| virtual void | moveBy (double dx , double dy ) |
| virtual int | rtti () const |
| virtual void | setActive (bool yes ) |
| virtual void | setAnimated (bool y ) |
| virtual void | setCanvas (Q3Canvas * c ) |
| virtual void | setEnabled (bool yes ) |
| virtual void | setSelected (bool yes ) |
| virtual void | setVelocity (double vx , double vy ) |
| virtual void | setVisible (bool yes ) |
| void | setX (double x ) |
| void | setXVelocity (double vx ) |
| void | setY (double y ) |
| void | setYVelocity (double vy ) |
| void | setZ (double z ) |
| void | show () |
| double | x () const |
| double | xVelocity () const |
| double | y () const |
| double | yVelocity () const |
| double | z () const |
| void | update () |
The Q3CanvasItem class provides an abstract graphic object on a Q3Canvas .
A variety of Q3CanvasItem 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. Q3CanvasItem is not intended for direct subclassing. It is much easier to subclass one of its subclasses, e.g. Q3CanvasPolygonalItem (the commonest base class), Q3CanvasRectangle , Q3CanvasSprite , Q3CanvasEllipse or Q3CanvasText .
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) ),和 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 Q3CanvasItem object can be moved in the x (), y () 和 z () dimensions using functions such as move (), moveBy (), setX (), setY () 和 setZ (). A canvas item can be set in motion, `animated', using setAnimated () and given a velocity in the x and y directions with setXVelocity () 和 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.
使用 Q3CanvasSprite or your own subclass of Q3CanvasSprite to create canvas items which are animated, i.e. which change over time.
The size of a canvas item is given by boundingRect ()。使用 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 Q3CanvasItem 。 canvas () function returns a pointer to the canvas which contains the canvas item.
Q3CanvasItem 提供 show () 和 isVisible () functions like those in QWidget .
Q3CanvasItem 还提供 setEnabled (), setActive () 和 setSelected () functions; these functions set the relevant boolean and cause a repaint but the boolean values they set are not used in Q3CanvasItem 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.
另请参阅 QtCanvas and Porting to Graphics View .
This enum is used to name the different types of canvas item.
| 常量 | 值 | 描述 |
|---|---|---|
Q3CanvasItem::Rtti_Item
|
0
|
Canvas item abstract base class |
Q3CanvasItem::Rtti_Ellipse
|
6
|
|
Q3CanvasItem::Rtti_Line
|
7
|
|
Q3CanvasItem::Rtti_Polygon
|
4
|
|
Q3CanvasItem::Rtti_PolygonalItem
|
2
|
|
Q3CanvasItem::Rtti_Rectangle
|
5
|
|
Q3CanvasItem::Rtti_Spline
|
8
|
|
Q3CanvasItem::Rtti_Sprite
|
1
|
|
Q3CanvasItem::Rtti_Text
|
3
|
构造 Q3CanvasItem on canvas canvas .
另请参阅 setCanvas ().
[虚拟]
Q3CanvasItem::
~Q3CanvasItem
()
销毁 Q3CanvasItem and removes it from its canvas.
[虚拟]
void
Q3CanvasItem::
advance
(
int
phase
)
The default implementation moves the canvas item, if it is animated (), by the preset velocity if phase is 1, and does nothing if phase 为 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.
另请参阅 Q3Canvas::advance () 和 setVelocity ().
Returns true if the canvas item is in motion; otherwise returns false.
另请参阅 setVelocity () 和 setAnimated ().
[pure virtual]
QRect
Q3CanvasItem::
boundingRect
() const
Returns the bounding rectangle in pixels that the canvas item covers.
另请参阅 boundingRectAdvanced ().
[虚拟]
QRect
Q3CanvasItem::
boundingRectAdvanced
() const
Returns the bounding rectangle of pixels that the canvas item will cover after advance(1) is called.
另请参阅 boundingRect ().
Returns the canvas containing the canvas item.
另请参阅 setCanvas ().
[pure virtual]
bool
Q3CanvasItem::
collidesWith
(const
Q3CanvasItem
*
other
) const
Returns true if the canvas item will collide with the other item after they have moved by their current velocities; otherwise returns false.
另请参阅 collisions ().
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 参数。
若 exact is true, the canvas items returned have been accurately tested for collision with the canvas item.
若 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 Q3CanvasItems, 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.
[pure virtual]
void
Q3CanvasItem::
draw
(
QPainter
&
painter
)
This abstract virtual function draws the canvas item using painter .
Shorthand for setVisible (false).
返回 true 若 Q3CanvasItem is active; otherwise returns false.
返回 true 若 Q3CanvasItem is enabled; otherwise returns false.
Returns true if the canvas item is selected; otherwise returns false.
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.
另请参阅 setVisible () 和 z ().
Moves the canvas item to the absolute position ( x , y ).
[虚拟]
void
Q3CanvasItem::
moveBy
(
double
dx
,
double
dy
)
Moves the canvas item relative to its current position by ( dx , dy ).
[虚拟]
int
Q3CanvasItem::
rtti
() const
Returns 0 ( Q3CanvasItem::Rtti_Item ).
Make your derived classes return their own values for rtti(), so that you can distinguish between objects returned by Q3Canvas::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 Q3CanvasItem found by Q3Canvas::at(), cast it to that type and call meaningful methods rather than acting upon the object based on its rtti value.
例如:
Q3CanvasItem* item; // Find an item, e.g. with Q3CanvasItem::collisions(). ... if (item->rtti() == MySprite::RTTI) { MySprite* s = (MySprite*)item; if (s->isDamagable()) s->loseHitPoints(1000); if (s->isHot()) myself->loseHitPoints(1000); ... }
[虚拟]
void
Q3CanvasItem::
setActive
(
bool
yes
)
Sets the active flag of the item to yes . If this changes the item's active state the item will be redrawn when Q3Canvas::update () is next called.
The Q3Canvas , Q3CanvasItem and the Qt-supplied Q3CanvasItem 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.
另请参阅 active ().
[虚拟]
void
Q3CanvasItem::
setAnimated
(
bool
y
)
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 () 和 setYVelocity ().
另请参阅 animated (), advance (),和 Q3Canvas::advance ().
[虚拟]
void
Q3CanvasItem::
setCanvas
(
Q3Canvas
*
c
)
设置 Q3Canvas upon which the canvas item is to be drawn to c .
另请参阅 canvas ().
[虚拟]
void
Q3CanvasItem::
setEnabled
(
bool
yes
)
Sets the enabled flag of the item to yes . If this changes the item's enabled state the item will be redrawn when Q3Canvas::update () is next called.
The Q3Canvas , Q3CanvasItem and the Qt-supplied Q3CanvasItem 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.
另请参阅 enabled ().
[虚拟]
void
Q3CanvasItem::
setSelected
(
bool
yes
)
Sets the selected flag of the item to yes . If this changes the item's selected state the item will be redrawn when Q3Canvas::update () is next called.
The Q3Canvas , Q3CanvasItem and the Qt-supplied Q3CanvasItem 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.
另请参阅 selected ().
[虚拟]
void
Q3CanvasItem::
setVelocity
(
double
vx
,
double
vy
)
Sets the canvas item to be in motion, moving by vx and vy pixels in the horizontal and vertical directions respectively.
另请参阅 advance (), setXVelocity (),和 setYVelocity ().
[虚拟]
void
Q3CanvasItem::
setVisible
(
bool
yes
)
Makes the canvas item visible if yes is true, or invisible if yes is false. The change takes effect when Q3Canvas::update () is next called.
另请参阅 visible ().
Moves the canvas item so that its x-position is x .
Sets the horizontal component of the canvas item's velocity to vx .
另请参阅 xVelocity (), setYVelocity (),和 setVelocity ().
Moves the canvas item so that its y-position is y .
Sets the vertical component of the canvas item's velocity to vy .
另请参阅 yVelocity (), setXVelocity (),和 setVelocity ().
Sets the z index of the canvas item to z . Higher-z items obscure (are in front of) lower-z items.
Shorthand for setVisible (true)。
[protected]
void
Q3CanvasItem::
update
()
Call this function to repaint the canvas's changed chunks.
Returns the horizontal position of the canvas item. Note that subclasses often have an origin other than the top-left corner.
另请参阅 setX ().
Returns the horizontal velocity component of the canvas item.
另请参阅 setXVelocity ().
Returns the vertical position of the canvas item. Note that subclasses often have an origin other than the top-left corner.
另请参阅 setY ().
Returns the vertical velocity component of the canvas item.
另请参阅 setYVelocity ().
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.
另请参阅 setZ ().