arbitrary是什么意思中文,arbitrary翻译

  

  详细描述   

  

  它为编写您自己的定制项目提供了一个轻量级的基础。这包括定义项目的几何图形、碰撞检测、其绘画实现以及通过其事件处理程序的项目交互100 . QGraphicsItem是图形视图框架的一部分   

  

  它为编写你的自定义项目提供了一个轻量级的基础。这包括定义项目的几何形状,碰撞检测,它的绘画实现,和通过事件处理程序的项目交互100 .图元类是图形视图框架的一部分。   

  

  为了方便起见,Qt为最常见的形状提供了一组标准的图形项目。   

  

  常用形状便利函数:   

  

  椭圆提供了一个椭圆项目椭圆项目QGraphicsLineItem提供了一个行项目直线项目QGraphicsPathItem提供了一个任意路径项路径项目QGraphicsPixmapItem提供了一个像素地图项目像素地图项目qgraphics多边形提供一个多边形项目多边形项目矩形提供了一个矩形项目矩形项目QGraphicsSimpleTextItem提供一个简单的文本标签项简单文本项目文本图元提供了一个高级文本浏览器项目文本项目项目的所有几何信息都基于其局部坐标系。项目的位置位置()是唯一不在本地坐标中操作的函数,因为它返回父坐标中的位置。图形视图坐标系详细描述了坐标系。   

  

  项目的所有几何信息都基于其局部坐标系统。项目的位置位置()是唯一不在局部坐标中操作的函数,因为它返回父对象坐标中的位置。图形视图坐标系统详细描述了坐标系统。   

  

  您可以通过调用setVisible()来设置一个项目是否应该可见(即绘制和接受事件)。隐藏项目也会隐藏其子项目。同样,您可以通过调用setEnabled()来启用或禁用某个项目。如果禁用某个项目,其所有子项目也将被禁用。默认情况下,项目既是可见的又是启用的。要切换某项是否被选中,首先通过设置项目可选择标志来启用选择,然后调用setSelected().通常,作为用户交互的结果,选择由场景切换。   

  

  setVisible()设置一个项目的可见性(例如,绘制和接受事件)。隐藏一个项目也会隐藏它的子项目。类似地,调用setEnabled()来使能一个项。如果你禁用一个项目,它的所有子项目也将被禁用。默认情况下,项目既可见又启用。要切换项目的选中状态,先设置项目可选择标志,再调用setSelected()。通常,选中状态切换是由场景负责,它是用户交互产生的结果。   

  

  要编写自己的图形项,首先创建图元类的子类,然后开始实现它的两个纯虚拟公共函数: boundingRect(),该函数返回一个自我提高训练法   

imate of the area painted by the item, and paint(), which implements the actual painting.

  

要编写自定义图形项,首先要创建QGraphicsItem的一个派生类,然后重写它的两个纯虚函数:一是boundingRect(),它返回项绘制区域的边界矩形,二是paint(),它具体完成绘制的工作。

  


  

For example:

  

class SimpleItem : public QGraphicsItem

  

{

  

public:

  

QRectF boundingRect() const override

  

{

  

qreal penWidth = 1;

  

return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,

  

20 + penWidth, 20 + penWidth);

  

}

  


  

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

  

QWidget *widget) override

  

{

  

painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);

  

}

  

};

  


  

The boundingRect() function has many different purposes. QGraphicsScene bases its item index on boundingRect(), and QGraphicsView uses it both for culling invisible items, and for determining the area that needs to be recomposed when drawing overlapping items. In addition, QGraphicsItem's collision detection mechanisms use boundingRect() to provide an efficient cut-off. The fine grained collision algorithm in collidesWithItem() is based on calling shape(), which returns an accurate outline of the item's shape as a QPainterPath.

  

boundingRect()函数有许多不同的用途,QGraphicsScene的项目索引基于它,QGraphicsView使用它来剔除不可见的项目,以及在绘制重叠项目时确定需要重新组合的区域。此外,QGraphicsItem的碰撞检测机制使用boundingRect()来提供一个有效的截止。collidesWithItem()中的细粒度碰撞算法基于调用shape(),它以QPainterPath的形式返回项目形状的精确轮廓。

  


  

QGraphicsScene expects all items boundingRect() and shape() to remain unchanged unless it is notified. If you want to change an item's geometry in any way, you must first call prepareGeometryChange() to allow QGraphicsScene to update its bookkeeping.

  

QGraphicsScene希望所有项目的boundingRect()和shape()保持不变,除非它被通知。如果你想以任何方式改变一个项目的几何形状,你必须首先调用prepareGeometryChange()来允许QGraphicsScene更新它的簿记。

  


  

Collision detection can be done in two ways:

  

Reimplement shape() to return an accurate shape for your item, and rely on the default implementation of collidesWithItem() to do shape-shape intersection. This can be rather expensive if the shapes are complex.Reimplement collidesWithItem() to provide your own custom item and shape collision algorithm.碰撞检测可以通过两种方式完成:

  

1. 重写shape()方法,为你的项目返回一个精确的形状,并依赖collidesWithItem()的默认实现来实现shape-shape交叉。如果形状很复杂,这可能是相当昂贵的。

  

2. 重新实现collidesWithItem()来提供你自定义项目和形状碰撞算法。

  


  

The contains() function can be called to determine whether the item contains a point or not. This function can also be reimplemented by the item. The default behavior of contains() is based on calling shape().

  

可以调用contains()函数来确定项目是否包含一个点。这个函数也可以由项目重新实现。contains()的默认行为是基于调用shape()。

  


  

Items can contain other items, and also be contained by other items. All items can have a parent item and a list of children. Unless the item has no parent, its position is in parent coordinates (i.e., the parent's local coordinates). Parent items propagate both their position and their transformation to all children.

  

项可以包含其他项,也可以被其他项包含。所有项都可以有一个父项和一个子项列表。除非项目没有父项,否则它的位置在父项的坐标中(即父项的局部坐标)。父项将它们的位置和变换传播给所有子项。

  

Transformations

  

QGraphicsItem supports projective transformations in addition to its base position, pos(). There are several ways to change an item's transformation. For simple transformations, you can call either of the convenience functions setRotation() or setScale(), or you can pass any transformation matrix to setTransform(). For advanced transformation control you also have the option of setting several combined transformations by calling setTransformations().

  

QGraphicsItem除了支持它的基础位置pos()外,还支持投影变换。有几种方法可以更改项的变换。对于简单的转换,你可以调用便利函数setRotation()或setScale(),或者你可以将任何转换矩阵传给setTransform()。对于高级变换控制,可调用setTransformations()来设置多个组合变换。

  


  

Item transformations accumulate from parent to child, so if both a parent and child item are rotated 90 degrees, the child's total transformation will be 180 degrees. Similarly, if the item's parent is scaled to 2x its original size, its children will also be twice as large. An item's transformation does not affect its own local geometry; all geometry functions (e.g., contains(), update(), and all the mapping functions) still operate in local coordinates. For convenience, QGraphicsItem provides the functions sceneTransform(), which returns the item's total transformation matrix (including its position and all parents' positions and transformations), and scenePos(), which returns its position in scene coordinates. To reset an item's matrix, call resetTransform().

  

项目转换从父项目到子项目累积,因此如果父项和子项都旋转90度,子项的总转换将为180度。类似地,如果项的父项被缩放到其原始大小的2倍,子项也将是原大小的2倍。项目的变换不会影响它自己的局部几何形状;所有几何函数(例如contains()、update()和所有映射函数)仍然在局部坐标中操作。为了方便起见,QGraphicsItem提供了sceneTransform()函数,它返回项目的总变换矩阵(包括它的位置和所有父元素的位置和变换),以及scenePos()函数,返回它在场景坐标系中的位置。要重置项目的矩阵,调用resetTransform()。

  

Certain transformation operations produce a different outcome depending on the order in which they are applied. For example, if you scale an transform, and then rotate it, you may get a different result than if the transform was rotated first. However, the order you set the transformation properties on QGraphicsItem does not affect the resulting transformation; QGraphicsItem always applies the properties in a fixed, defined order:

  

The item's base transform is applied (transform())The item's transformations list is applied in order (transformations())The item is rotated relative to its transform origin point (rotation(), transformOriginPoint())The item is scaled relative to its transform origin point (scale(), transformOriginPoint())某些变换操作根据应用它们的顺序产生不同的结果。例如,如果你缩放一个变换,然后旋转它,你可能会得到与先旋转变换不同的结果。但是,你在QGraphicsItem上设置变换属性的顺序不会影响产生的变换;QGraphicsItem总是以固定的、定义好的顺序应用属性:

  

l 项目的基本变换被应用(transform())

  

l 项目的变换列表按顺序被应用 (transformations())

  

l 项目相对于其变换原点被旋转(rotation(), transformOriginPoint())

  

l 项目相对于其变换原点被缩放(scale(), transformOriginPoint())

  


  

Painting

  

The paint() function is called by QGraphicsView to paint the item's contents. The item has no background or default fill of its own; whatever is behind the item will shine through all areas that are not explicitly painted in this function. You can call update() to schedule a repaint, optionally passing the rectangle that needs a repaint. Depending on whether or not the item is visible in a view, the item may or may not be repainted; there is no equivalent to QWidget::repaint() in QGraphicsItem.

  

QGraphicsView调用paint()函数来绘制项目的内容。该项目没有自己的背景或默认填充;无论项目背后是什么,都会在所有未明确绘制的区域中发光。你可以调用update()来安排重绘,可以选择传递需要重绘的矩形。根据项目在视图中是否可见,项目可以重绘或不重绘;在QGraphicsItem中没有与QWidget::repaint()等价的方法。

  


  

Items are painted by the view, starting with the parent items and then drawing children, in ascending stacking order. You can set an item's stacking order by calling setZValue(), and test it by calling zValue(), where items with low z-values are painted before items with high z-values. Stacking order applies to sibling items; parents are always drawn before their children.

  

项目由视图绘制,从父项目开始,然后按堆升序绘制子项目。你可以通过调用setZValue()来设置项目的堆序,并通过调用zValue()来测试它,其中低z值的项目先于高z值的项目绘制。堆序适用于兄弟项;父总是先于孩绘制。

  


  

Sorting

  

All items are drawn in a defined, stable order, and this same order decides which items will receive mouse input first when you click on the scene. Normally you don't have to worry about sorting, as the items follow a "natural order", following the logical structure of the scene.

  

所有的项目都是按照一个确定的、稳定的顺序绘制的,并且当你点击场景时,同样的顺序决定了哪些项目将首先接受鼠标输入。通常情况下,你不必担心排序,因为项目遵循“自然顺序”,遵循场景的逻辑结构。

  


  

An item's children are stacked on top of the parent, and sibling items are stacked by insertion order (i.e., in the same order that they were either added to the scene, or added to the same parent). If you add item A, and then B, then B will be on top of A. If you then add C, the items' stacking order will be A, then B, then C.

  


  

一个项目的子项目堆叠在父项目之上,而同级项目则按插入顺序堆叠(即,按照它们被添加到场景或添加到同一个父项目的顺序)。如果你添加项目A,然后添加项目B,那么项目B将在项目A之上。如果然后你添加物品C,物品的堆序将是A,然后是B,然后是C。

  


  

This example shows the stacking order of all limbs of the robot from the Drag and Drop Robot example. The torso is the root item (all other items are children or descendants of the torso), so it is drawn first. Next, the head is drawn, as it is the first item in the torso's list of children. Then the upper left arm is drawn. As the lower arm is a child of the upper arm, the lower arm is then drawn, followed by the upper arm's next sibling, which is the upper right arm, and so on.

  

这个示例显示了机器人所有肢体的堆序(拖放机器人示例)。躯干是根项目(所有其他项目都是躯干的子项目或后代项目),所以它是首先绘制的。接下来,画出头部,因为它是躯干的子列表中第一项。然后绘制左上方的手臂。由于小臂是上臂的子臂,于是绘制小臂,然后是上臂的下一个兄弟,也就是右上方的手臂,以此类推。

  


  

For advanced users, there are ways to alter how your items are sorted:

  

You can call setZValue() on an item to explicitly stack it on top of, or under, other sibling items. The default Z value for an item is 0. Items with the same Z value are stacked by insertion order.You can call stackBefore() to reorder the list of children. This will directly modify the insertion order.You can set the ItemStacksBehindParent flag to stack a child item behind its parent.对于高级用户,有一些方法可以改变条目的排序方式:

  

l 你可以对一个条目调用setZValue()来显式地将它堆叠在其他同级条目之上或之下。项目的默认Z值是0。具有相同Z值的项将按插入顺序堆叠。

  

l 你可以调用stackBefore()来重新排列子列表。这将直接修改插入顺序。

  

l 你可以设置ItemStacksBehindParent标志来将子项目堆叠在其父项目之后。

  


  

The stacking order of two sibling items also counts for each item's children and descendant items. So if one item is on top of another, then all its children will also be on top of all the other item's children as well.

  

两个兄弟项目的堆叠顺序也可以计算每个项目的子项目和后代项目。因此,如果一个项目在另一个项目之上,那么它的所有子项目也将在另一个项目的所有子项目之上。

  


  

Events

  

QGraphicsItem receives events from QGraphicsScene through the virtual function sceneEvent(). This function distributes the most common events to a set of convenience event handlers:

  

contextMenuEvent() handles context menu eventsfocusInEvent() and focusOutEvent() handle focus in and out eventshoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent() handles hover enter, move and leave eventsinputMethodEvent() handles input events, for accessibility supportkeyPressEvent() and keyReleaseEvent() handle key press and release eventsmousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and mouseDoubleClickEvent() handles mouse press, move, release, click and doubleclick eventsQGraphicsItem通过虚拟函数sceneEvent(),从QGraphicsScene接收事件。这个函数将最常见的事件分配给一组便利事件处理程序:

  

contextMenuEvent()处理上下文菜单事件focusInEvent()和focusOuteevent()处理焦点进入和离开事件hoverEnterEvent()、hoverMoveEvent()和hoverLeaveEvent()处理悬停进入、移动和离开事件inputMethodEvent()处理输入事件,以获得可访问性支持keyPressEvent()和keyReleaseEvent()处理按键按下和释放事件mousePresseEvent (), mouseMoveEvent (), mouseReleaseEvent(),和mouseDoubleClickEvent()处理鼠标按压,移动,释放,点击和双击事件。

  

You can filter events for any other item by installing event filters. This functionality is separate from Qt's regular event filters (see QObject::installEventFilter()), which only work on subclasses of QObject. After installing your item as an event filter for another item by calling installSceneEventFilter(), the filtered events will be received by the virtual function sceneEventFilter(). You can remove item event filters by calling removeSceneEventFilter().

  

你可以安装事件过滤器,过滤任何其他项目的事件。这个功能与Qt的常规事件过滤器是分开的(参见QObject::installEventFilter()),后者只在QObject的子类上工作。通过调用installSceneEventFilter()将你的项目安装为另一个项目的事件过滤器后,被过滤的事件将由虚拟函数sceneEventFilter()接收。你可以通过调用removeSceneEventFilter()来删除项目事件过滤器。

  


  

Custom Data

  

Sometimes it's useful to register custom data with an item, be it a custom item, or a standard item. You can call setData() on any item to store data in it using a key-value pair (the key being an integer, and the value is a QVariant). To get custom data from an item, call data(). This functionality is completely untouched by Qt itself; it is provided for the user's convenience.

  

有时,将自定义数据注册到一个项(无论是自定义项还是标准项)是很有用的。可以对任何项调用setData(),使用键值对(键是整数,值是QVariant)在其中存储数据。要从项目中获取自定义数据,调用data()。Qt本身完全没有触及这个功能;它是为用户方便而提供的。

  


  

至此,图形视图框架的三部分都翻译完成。

相关文章