Graphics View provides an interface for managing and interacting with a large number of custom 2D graphical elements, as well as for viewing components for visualizing these elements, and supports scaling and rotation. What we normally call Linux's KDE desktop environment is based on Graphics View (although the new version of KDE has a tendency to migrate to QML).
The Graphics View Framework contains a complete set of event systems that can be used to interact with the elements of the scene in a double precision. These elements also support keyboard events, mouse events, and so on. Graphics View uses the BSP tree (the Binary space Partitioning, which is widely used in graphical data structures) to provide very fast element discovery, and because of this, Can realize a real-time display mechanism of millions of orders of magnitude elements.
Graphics View was first introduced in QT 4.2 to replace the Qcanvas in QT 3. Of course, in the latest Qt5, QT3 's code is no longer available (although, to some extent, QT4 can still use these legacy codes).
Graphics View is a framework for the MV architecture based on the element (item). It can be divided into three parts: element item, Scene scene and view.
Element-based means that each of its components is a separate element. This is different from the Qpainter state machine mechanism we talked about before. Recall that using a qpainter drawing is mostly a process-oriented description: First, use DrawLine () to draw a straight line, and then use DrawPolygon () to draw a polygon. For Graphics View, the same process can be, first create a scene (scene), and then create a straight object and a Polygon object, and then use the scene's Add () function, the line and Polygon added to the scene, and finally through the view of the observation, you can see. At first glance, the latter seems more complicated, but if you have thousands of lines and polygons in your image, it's much easier to manage them than to manage Qpainter's drawing statements. Also, these graphics objects are more in line with object-oriented design requirements: A very complex graphic can be easily reused.
MV architecture means that Graphics view provides a model and a view (just like the MVC architecture, but the MV architecture is less than a C component). The so-called model is the object we add, and the view is the viewport in which we observe these objects. The same model can be viewed from different perspectives by many views, which is a common requirement. This is difficult to achieve with qpainter, which requires very complex computations, and Graphics View can be easily implemented.
Graphics View provides qgraphicsscene as a scene, which allows us to add graphics to the space equivalent to the entire world; Qgraphicsview as a viewport, which is our observation window, the equivalent of a camera's frame, this frame can cover the entire scene, It can also be part of a scene; Qgraphicsitem as a graphical component to add to the scene, Qt has a lot of graphics built into it, such as lines, polygons, etc., which inherit from Qgraphicsitem.
Let's look at the use of Graphics View through a piece of code.
int main (int argc, char *argv[])
{
qapplication app (argc, argv);
Qgraphicsscene scene;
Scene.addline (0, 0,);
Qgraphicsview view (&scene);
View.setwindowtitle ("Graphics View");
View.resize (+);
View.show ();
return app.exec ();
}
This code is simple: first create a scene, which is the Qgraphicsscene object. We then used the AddLine () function to add a line to the scene, starting point and end coordinates (0, 0) and (150, 150) respectively. As you can imagine, this is a diagonal of a square with a long 150px edge. With these two steps, we already have scenes and elements. After that, we create a Graphicsview object that binds to a scene (that is, the scene object we created earlier). Note that Qgraphicsscene is not a subclass of Qwidget, so the constructor is not the called Qgraphicsview (Qwidget *parent). Next, we can run the code:
As we can see, this line is automatically displayed in the center of the view. This does not require us to carry out any additional code. If you don't want to do this, we can set the Scenerect () property for scene:
Qgraphicsscene scene;
Scene.setscenerect (0, 0,,);
Scene.addline (0, 0,);
Qgraphicsview view (&scene);
View.setwindowtitle ("Graphics View");
View.resize (+);
View.show ();
Not only that, we also removed the view.resize () line. The Qgraphicsscene Scenerect property is for Qgraphicsview to determine the default scroll bar area for the view and assists Qgraphicsscene in managing the element index. The reason to remove the view.resize () line is because we let the system determine the minimum size of the view (otherwise, we need to manually consider the size of the window title bar and so on).