Graphics view cabinet of QT (1/3)

Source: Internet
Author: User

Graphics view provides an interface that can manage a large number of custom 2D graphical items and interact with them. A view widget can draw these items, it also supports rotation and scaling. The Cabinet also contains an event propagation structure. For these items in scene, it has dual-precision interaction capabilities. Items can handle Keyboard Events, move, release, and double-click events by mouse, or track mouse movements. Graphics view uses the BSP tree to quickly search for items. With this technology, it can be used to plot large-scale scenarios in real time, or even millions of items. Graphics view is referenced in QT 4.2, which replaces its predecessor qcanvas.

Architecture of graphics View

Graphics view provides a programming method similar to QT Model-View. Multiple Views can monitor the same scenario, and the scenario contains multiple items with multiple geometric shapes.
Scenario
Qgraphicsscene indicates the scenario in graphics view. It has the following responsibilities:
Provides a quick interface for managing a large number of items.
Spread events to each item.
Manage the item status, such as selection and focus processing.
Provides the untransformed rendering function, which is mainly used for printing.
The scenario is the container of the qgraphicsitem object. Call qgraphicsscene: additem () to add these items to the scene. You can use multiple search functions to obtain specific items. Qgraphicsscene: the items () and many of its heavy-duty functions can obtain the items related to vertex, rectangle, polygon, vector path, and so on. Qgraphicsscene: itemat () returns the top of a specific item. All item lookup functions are returned in an output stack sequence (that is, the first returns the top and the last returns the bottom ).
Qgraphicsscene scene;
Qgraphicsrectitem * rect = scene. addrect (qrectf (0, 0, 100,100 ));
Qgraphicsitem * Item = scene. itemat (50, 50 );
// Item = rect;
The event propagation structure of qgraphicsscene delivers scenario events to items and manages the transfer between multiple items. If the scene receives the press event at a certain position, the scene will deliver the event to the item at that position. Qgraphicsscene also manages certain item states, such as selection and focus. You can call qgraphicsscene: setselectionarea () to select items. It must provide any shape as a parameter. This function also serves as a basis for implementing the rubber band selection function in qgraphicsview. To obtain these selected items, call qgraphicsscene: selecteditem (). Another State processing is whether an item has the keyboard input focus. You can call qgraphicsscene: setfocusitem () or qgraphics: setfocus () to set the focus. You can also use qgraphicsscene: focusitem () to obtain the item with the current focus. Finally, qgraphicsscene allows you to send some scenes to the drawing device for rendering by calling the qgraphicsscene: render () function.
View
Qgraphicsview provides a view component that visualizes the content in a scenario. You can link multiple views to the same scenario and provide several views for the same dataset. A widget is a scroll area that provides a scroll bar to browse large scenes. To use OpenGL, you should call qgraphicsview: setviewport () to set a qglwidget as a view. The view receives input events from the keyboard and mouse. before these events are sent to the scene, the events are properly translated (the events are converted to the corresponding scene coordinates ).
Using the conversion matrix, qgraphicsview: matrix (), the view can be used to transform the coordinate system of the scenario. This allows advanced navigation features such as scaling and rotation. For convenience, qgraphicsview also provides functions for Coordinate Conversion between views and scenarios: qgraphicsview: maptoscene (), qgraphicsview: mapforscene ().

The item
Qgraphicsitem is the basic class of graphics items in a scenario. Graphics view provides some standard items for typical shapes. Like qgraphicsrectitem, qgraphicsellipseitem, and qgraphicstextitem, the most useful qgraphicsitem features are also effective when you write custom items. In addition, qgraphicsitem supports the following features:
* Click, move, release, double-click, hover, and scroll wheel events. A menu event is displayed.
* Enter the keyboard focus and Keyboard Events.
* Drag
* Group, including parent-child relationship, using qgraphicsitemgroup
* Collision Detection
Like qgraphicsview, items is located in the local coordinate system and provides many tool functions for coordinate transformation between items and scenarios. In addition, like qgraphicsview, it uses a matrix to transform its coordinate system: qgraphicsitem: matrix (). It is useful for rotating and scaling a single item.
Items can contain other items (child ). The conversion of the parent items is inherited by its descendants. However, all its functions (that is, qgraphicsitem: Contains (), qgraphicsitem: boundingrect (), qgraphicsitem: collideswith () won't accumulate these conversions, still working in local coordinates.
Qgraphicsitem supports collision detection through qgraphicsitem: shape (), qgraphicsitem: collidewith. Both are virtual functions. Return the shape of your item (expressed in the local coordinate qpainterpath) from shape (). qgraphicsitem will process all collision detection for you. If you want to provide your own collision detection, you should re-implement qgraphicsitem: collidewith ().

From: http://www.cppblog.com/yuanyajie/archive/2007/09/26/32961.html

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.