Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

The item's coordinate system

Each item has its own coordinate system. In our example of Post-it notes, the content of each note is defined relative to the top-left corner of the note. No matter how you move or rotate the item, these coordinates remain the same. The coordinates of a drawn object can usually be passed to the constructor of the class, like we did in our demo project, or to a special setter function (for example, rectItem->setRect(0, 10, 20, 25)). These are coordinates in the item's coordinate system.

Some classes, such as QGraphicsSimpleTextItem, do not provide the ability to change the coordinates of the content, so they're always positioned at the origin of the item's coordinate system. This is not a problem at all; as we'll see next, there are ways to change the visible position of the content.

If you try to create your own graphics item class (we'll get to it later in this chapter), you'll need to implement the paint() and boundingRect() functions, and they always operate in the item's coordinate system. That's right, when you're painting the content, you can just pretend that your item will never be moved or transformed. When that actually happens, Qt will take care of transforming paint operations for you. Additionally, coordinates in any events the item receives (for example, coordinates of a mouse button click) are expressed in the item's coordinate system.