QT Learning Path (m): Graphics View Framework

Source: Internet
Author: User

Now basically also has been to the end of the 2D drawing part, the so-called plays are in the final finale, now we have to look at the drawing part of the most powerful graphics View. We often say that the KDE desktop, a new version of the KDE desktop, is built on the basis of graphics view, which shows its strength.

QT's White paper reads: "QT Graphics view provides a flat surface for managing and interacting with a large number of custom 2D graphics objects, and a view widget for visual display objects, and supports scaling and rotation functions." Graphics View uses a BSP (binary space partition) tree to find objects very quickly, so even large scenes that contain millions of objects can be graphically displayed in real time. ”

Graphics View is a framework for M-V architecture based on the item.

Based on the item, each of its components is an item. This is different from the Qpainter state machine. Recall that using Qpainter drawing is mostly a process-oriented description, first using DrawLine () to draw a straight line, and then using DrawPolygon () to draw a polygon, and for graphics view, the same process can be, First create a scene scene, then create a line object and a Polygon object, and then use the scene Add () function to add line and polygon to the scene, and finally through the viewport view can be seen. At first glance, the latter seems more complicated, but if your image contains thousands of lines and polygons, it is much easier to manage these objects than to manage qpainter draw statements. Also, these graphics objects are more in line with object-oriented design requirements: A very complex graphic can be easily reused.

The M-V architecture means that Graphics view provides a model and a view. The so-called model is that we add a variety of objects, the so-called view is that we observe these objects of the viewport. The same model can be viewed from a number of views from different perspectives, which is a common requirement. This is difficult to achieve with qpainter, which requires very complex computations, and Qt's graphics view can be easily implemented.

Graphics View provides a qgraphicsscene as a scene, that is, the space we add to the graph, the equivalent of the whole world; a qgraphicsview as the viewport, the window we observe, the equivalent of the camera's frame, The frame can cover the entire scene, or it can be part of the scene, some qgraphicsitem as a graphical component for scene to add, and Qt has a lot of built-in graphics, such as line, polygon, all inherited from Qgraphicsitem.

Now let's take a look at the code:

#include <QtGui>
class DrawApp : public QWidget {
public:
   DrawApp();
protected:
        void paintEvent(QPaintEvent *event);
};
DrawApp::DrawApp()
{
}
void DrawApp::paintEvent(QPaintEvent *event)
{
 QPainter painter(this);
 painter.drawLine(10, 10, 150, 300);
}
int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 QGraphicsScene *scene = new QGraphicsScene;
 scene->addLine(10, 10, 150, 300);
 QGraphicsView *view = new QGraphicsView(scene);
 view->resize(500, 500);
 view->setWindowTitle("Graphics View");
 view->show();
 DrawApp *da = new DrawApp;
 da->resize(500, 500);
 da->setWindowTitle("QWidget");
 da->show();
 return a.exec();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.