Graphics view provides an interface for managing and interacting with a large number of custom 2D graphic elements. It also provides an observation component for visual display of these elements, it also supports scaling and rotation.
Description; graphics view framework contains a complete Event System, which can be used to perform double-precision interaction with elements in the scenario.
Mutual. These elements also support Keyboard Events and mouse events. Graphics view uses the BSP tree (Binary Space
Partitioning tree, which is a data structure widely used in graphics) to provide fast element development.
In this way, we can implement a real-time display mechanism of millions of elements.
Version Description: Graphics view was initially introduced in QT 4.2 to replace qcanvas in QT 3. Of course, in the latest qt5, The qt3 code can no longer be used (although to some extent, qt4 can still use the legacy code ).
Information Description:Graphics view is a framework of element-based MV architecture. It can be divided into three parts: element item,Scenario scene and view.
Principles:
"""
Element-based means that each component is an independent element. This is what we mentioned earlier.
The qpainter state machine mechanism is different. Recall that the use of qpainter plotting mostly uses a process-oriented profile.
Method: draw a straight line using drawline () and draw a polygon using drawpolygon. For
Graphics view. The same process can be: first create a scenario (scene), and then create a line Object
And a polygon object, and then use the add () function of the scenario to add straight lines and polygon to the scene.
Figure to see. At first glance, the latter seems more complex, but if your image contains
It is much easier to manage thousands of straight lines, polygon, and other objects than to manage qpainter draw statements. And
In addition, these graphic objects are more in line with the object-oriented design requirements: a very complex image can be easily reused.
The MV architecture means that graphics view provides a model and a view (just like the MVC Architecture
The MV architecture is less than the component C ). Models are the objects we add.
View is the view of these objects. The same model can be implemented by multiple views from different angles.
Observe that this is a common requirement. It is difficult to use qpainter to achieve this, which requires complicated computing.
Graphics view can be easily implemented.
Graphics view provides qgraphicsscene as a scenario, that is, the space that allows us to add images is equivalent
The whole world; qgraphicsview is used as the viewport, that is, our observation window, which is equivalent to the camera's scene frame,
This scenario frame can cover the entire scenario or be part of the scenario. qgraphicsitem is used as a graphical component,
In order to add the scene to the scene, QT has a lot of built-in graphics, such as straight lines, polygon, etc., they are inherited from
Qgraphicsitem.
"""
From pyqt4.qtgui import *
From pyqt4.qt import *
From pyqt4 import qtgui, qtcore
From pyqt4.qtcore import *
Import sys
Def main ():
APP = qtgui. qapplication (SYS. argv)
Scene = qgraphicsscene ()
Scene. addline (150,150)
View = qgraphicsview (scene)
View. setwindowtitle ('graphics view ')
View. resize( 500,500)
View. Show ()
Sys.exit(app.exe C _())
If _ name _ = '_ main __':
Main ()
:
----------------------
#-*-Coding: UTF-8 -*-
# Python: 2.x
_ Author _ = 'admin'
From pyqt4.qtgui import *
From pyqt4.qt import *
From pyqt4 import qtgui, qtcore
From pyqt4.qtcore import *
Import sys
Def main ():
APP = qtgui. qapplication (SYS. argv)
Scene = qgraphicsscene () # first create a scenario, that is, the qgraphicsscene object.
Scene. addline (150,150,) # The addline () function adds a straight line to the scenario.
Scene. setscenerect (0, 0,300,300)
# The coordinates of the start and end points are (0, 0) and (150,150), respectively)
View = qgraphicsview (scene) # graphicsview object, bound to a scenario
# Qgraphicsscene is not a subclass of qwidget. Therefore, this constructor is not the called qgraphicsview (qwidget * parent)
View. setwindowtitle ('graphics view ')
# View. Resize (500,500)
View. Show ()
Sys.exit(app.exe C _())
If _ name _ = '_ main __':
Main ()
""""
A row of view. Resize () is removed. The scenerect attribute of qgraphicsscene is provided
Qgraphicsview determines the default scroll bar area of the view and helps qgraphicsscene manage element indexes.
The reason why the view. Resize () Row is removed is that we asked the system to determine the minimum size of the view (otherwise, I
You need to manually set the size of the window title bar and so on ).
"""