Now it's almost the end of the 2D plot. The so-called major scenes are all on the final axis. Now let's look at the most powerful Graphics View in the plot. We often say that the new version of KDE Desktop is based on Graphics View, showing its strength. In the White Paper of Qt, "Qt Graphics View provides a View widget for managing and interacting with a large number of custom 2D graphic objects, as well as visual display objects, and supports scaling and rotation. Graphics View uses BSP binary Space Division) the tree can locate objects very quickly, so even large scenarios that contain millions of objects can be visually displayed in real time ." Graphics View is a framework of item-based M-V architecture. Each component of an item is an item. This is different from the QPainter state machine. Recall that the use of QPainter for plotting is mostly a process-oriented description method. First, draw a straight line using drawLine (), and then draw a polygon using drawPolygon; for Graphics View, the same process can be: first create a scenario scene, then create a line object and a polygon object, and then use add () of scene () the function adds line and polygon to scene, and you can see it through the view in the view. At first glance, the latter seems more complex, but if your image contains thousands of straight lines, polygon, and so on, managing these objects is much easier than managing the draw Statement of QPainter. In addition, these graphic objects are more in line with the object-oriented design requirements: a very complex image can be easily reused. The M-V architecture means that Graphics View provides a model and a view. The so-called model is the various objects we add, the so-called view is the view of these objects. The same model can be observed by many views from different perspectives, which is a common requirement. It is difficult to implement this with QPainter, which requires complicated computing, and Graphics View of Qt can be easily implemented. Graphics View provides a QGraphicsScene scenario, that is, the space for adding images is equivalent to the whole world; a QGraphicsView is used as the viewport, that is, the window we observe, which is equivalent to the camera's framing frame, this scenario frame can cover the entire scenario or be part of the scenario. Some QGraphicsItem are used as graphical components for scene addition. Qt has many built-in graphics, such as line and polygon, all are inherited from QGraphicsItem. Next let's take a look at the code: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/># Include <QtGui>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> class DrawApp: public QWidget {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> public:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> DrawApp ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> protected:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> void paintEvent (QPaintEvent * event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> DrawApp: DrawApp ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> void DrawApp: paintEvent (QPaintEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> QPainter painter (this );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> painter. drawLine (10, 10,150,300 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> int main (int argc, char * argv [])
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> QApplication a (argc, argv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> QGraphicsScene * scene = new QGraphicsScene;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> scene-> addLine (10, 10,150,300 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> QGraphicsView * view = new QGraphicsView (scene );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> view-> resize (500,500 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> view-> setWindowTitle (" Graphics View ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> view-> show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> DrawApp * da = new DrawApp;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> da-> resize (500,500 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> da-> setWindowTitle (" QWidget ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> da-> show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/> return a.exe c ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R6364025-0.gif "/>} To highlight the key point, we directly include the QtGui, but we do not recommend this in actual applications. Two implementation methods are provided here: DrawApp uses the technology we introduced earlier to rewrite the paintEvent () function. Here we will not repeat the implementation in main () function. First, we create a QGraphicsScene as the scenario, and then add a straight line to scene, so that we can put the graphic components we need into scene. Create a QGraphicsView object for observation. In this way, we use Graphics View to build a simple application. Run this program to see the result: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R6361256-36.png "/> 650) this. width = 650;" onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R63639B-37.png "/> The first graph is Graphics View, and the second graph is DrawApp. Although the two straight lines are the same coordinates, the DrawApp draws a straight line based on the original coordinates, while the Graphics View automatically centers the line in the view viewport after drawing a straight line based on the coordinates. You can drag the Graphics View to see that the straight line is always centered. This is just a simple comparison, but you can see the powerful Graphics View function. This is only the center operation. If you use QPainter, you need a lot of computing! Of course, if you do not need this kind of center, Graphics View can also be displayed as drawn by QPainter.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/241186