Primitive Operations (Theory)

Graphic element operation theory knowledge

Main features of the Graphics View framework structure

  1. In the Graphics View frame structure, the system can use the anti-aliasing and OpenGL tools of the Qt drawing system to improve drawing performance.
  2. Graphics View supports event propagation architecture, which can double the interaction ability of graphics elements in the scene (scene), and graphics elements can handle keyboard events and mouse events. Among them, the mouse events include the mouse being pressed, moved, released and double-clicked, and the movement of the mouse can also be tracked.
  3. In the Graphics View framework, fast primitive lookup is provided through Binary Space Partitioning (BSP), so that large scenes containing millions of primitives can be displayed in real time.

Three elements of the Graphics View framework

The Graphics View framework structure mainly includes three classes, namely scene class (QGraphicsScene), view class (QGraphicsView) and primitive class (QGraphicsItem), collectively referred to as "three elements". Among them, the scene class provides a container for managing many primitives located in it, and the view class is used to display the primitives in the scene. A scene can be represented by multiple views, and a scene includes multiple geometric figures.

1. Scene class: QGraphicsScene class

  • It is a container for placing primitives. It is invisible by itself and must be displayed and interoperated with the outside world through the view class connected to it.
  • A primitive can be added to the scene through QGraphicsScene::addItem(). Primitives can be retrieved by several functions.
  • QGraphicsScene::items() and some overloaded functions can return all primitives intersected by points, rectangles, polygons or vector paths. QGraphicsScene::itemAt() returns the top-level primitive at the specified point. (such as selection and focus handling), provide transformation-free drawing functionality (such as printing), etc.
  • The event propagation architecture sends scene events to primitives and also manages event propagation between primitives. If the scene receives a mouse click event at a certain point, the scene will pass the event to the primitive at that point.
  • Manage the state of individual primitives (such as selection and focus handling). The primitive can be selected through the QGraphicsScene::setSelectionArea() function, and the selection area can be of any shape, represented by QPainterPath. To get the list of currently selected primitives, you can use the QGraphicsScene::selectedItems() function. You can set the focus of the primitive through the QGraphicsScene::setFocusItem() function or the QGraphicsScene::setFocus() function, and use the QGraphicsScene::focusItem() function to obtain the currently focused primitive.

2. View class: QGraphicsView class

  • It provides a visual window for displaying primitives in the scene. There can be multiple views in the same scene, or several different views for the same dataset.
  • QGraphicsView is a scrollable widget that provides scroll bars to browse large scenes. If you need to use OpenGL, you can use the QGraphicsView::setViewport() function to set the view to a QGLWidget.
  • The view receives keyboard and mouse input events and translates them into scene events (converting coordinates to scene coordinates). Use the transformation matrix function QGraphicsView::matrix() to transform the coordinates of the scene to achieve scene scaling and rotation. QGraphicsView provides QGraphicsView::mapToScene() and QGraphicsView::mapFromScene() functions for conversion with the coordinates of the scene.

3. Graphics element class: QGraphicsItem class

It is the base class of each primitive in the scene, and various primitive classes can be inherited on the basis of it. Qt has presets including straight lines (QGraphicsLineItem), ellipses (QGraphicsEllipseItem), text primitives (QGraphicsTextItem), rectangles ( QGraphicsRectItem), etc. Of course, you can also implement a custom graphic element class based on the QGraphicsItem class, that is, users can inherit QGraphicsItem to implement graphic elements that meet their needs.

QGraphicsItem mainly has the following functions.

  • Handles mouse down, move, release, double click, hover, wheel and right click menu events.
  • Handle keyboard input events.
  • Handle drag events.
  • grouping.
  • Impact checking.

coordinate

Scene coordinates:
  • Scene coordinates are the base coordinate system for all primitives. The scene coordinate system describes the top-level primitives, and each primitive has scene coordinates and a corresponding containing box. The origin of the scene coordinates is in the center of the scene, the origin of the coordinates is the positive direction of the x-axis to the right, and the positive direction of the y-axis is downward.
  • The coordinate system of the QGraphicsScene class takes the center as the origin (0,0)
view coordinates
  • View coordinates are the coordinates of the widget. The unit of view coordinates is pixels. The upper left corner of the QGraphicsView view is (0,0), the positive direction of the x-axis is to the right, and the positive direction of the y-axis is downward. All mouse events initially use view coordinates.
  • The QGraphicsView class inherits from the QWidget class, so it, like other QWidget classes, uses the upper left corner of the window as the origin of its own coordinate system

primitive coordinates

  • Primitives use their own local coordinates. This coordinate system usually has the origin at the center of the primitive, which is also the origin of all transformations. The coordinate direction of the primitive is that the positive direction of the x-axis is to the right, and the positive direction of the y-axis is downward. After creating the primitive, just pay attention to the coordinates of the primitive, and QGraphicsScene and QGraphicsView will complete all the transformations.
  • The coordinate system of the QGraphicsItem class is based on this coordinate system when calling the paint() function of the QGraphicsItem class to redraw the drawing element

coordinate transformation

The Graphics View framework provides a variety of coordinate transformation functions:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44248637/article/details/130102962