Qt Graphics View Frame

Source: Internet
Author: User

These days to do camouflage design related programming with a lot of qpainter related operations, wrote a lot of QT code let me have a further understanding of QT. Recently looked at Qt Demos and Examples found in the graphics view there is a elastic node example, the program only defines the background and the small ball drawing operations to complete the complex mouse, keyboard and other interactions. While I marvel at the power of QT, I also decided to learn. The QT Graphics View Framework was used in a series of operations that looked up the help document for this.

The Graphics View Framework provides an interface for managing and interacting with a large number of user-defined 2D graphics objects, and a View widget (qgraphicsview) user to visualize these objects, supporting zoom and rotate. This framework contains an event propagation framework that allows the interaction of objects in a scene with double precision. Object keyboard, mouse down, move, release, double-click events are already defined, and can track the mouse movement.

Graphics View Architecture

All the item is drawn to a scene for display, this scene is Qgraphicsscene, this scene has the following functions:

1, to manage a large number of item provides a quick interactive interface.

2. Be able to pass keyboard, mouse and other events to each item.

3. You can manage the status of item, such as selection, focus, etc.

4, provides the non-deformation of the drawing, mainly used to print the results of painting.

The scene is like an easy to include all items, you can add objects through the AddItem (), you can find objects through item (), Itemat () returns the top item, all item is arranged in descending order. The first one added at the top of the stack, the last one added at the bottom of the stack.

The Qgraphicsscene event delivery architecture is very power-able to deliver events from the scene to the appropriate item, such as a mouse click at a point where scene can pass the event to item at that point (the top item), The item is then selected or executed, depending on the handling of the Mousepressevent function of item.

Qgraphicsscene, you can use Setselectionarea () to select many of the item in the area, just call this function you can get the result of the selected item, it is very powerful, if you want to write the function mouse Select the area of the selected item, You need to traverse through each item, and for each item to determine whether it is all in the area, so the item will surround the kernel, the range area and so on variables to represent. Think about it and feel real trouble. This function really saves developers a lot of time.

View

Qgraphicsview provides widgets for display, where you can connect multiple view to the same scene, provide multiple viewports for the same data, support OpenGL for Viewports, and even use qglwidget as a viewport. Just call Qgraphicsview::setviewport (). The view receives the interactive event and turns the coordinate transformation into scene event. Multiple functions, such as Qgraphicsview::maptoscene (), Qgraphicsview::mapfromscene (), can convert a coordinate system between view and scene.

Qgraphicsscene scene;
Mypopulatescene (&scene);
Qgraphicsview view (&scene);
View.show ();

Item

Qgraphicsitem is the base class for all objects in the scene, Graphicsview also provides some standard items, such as Rectangle Qgraphicsrectitem, ellipse qgraphicsellipseitem, and text, Qgraphicstextitem, most of all, of course, your custom Item,item supports the following actions:

1, mouse click, move, Release, double-click, fly over, scroll wheel, menu events.

2, keyboard input.

3. Dragging and dragging

4, Group, can be through the parent-child relationship or Qgraphicsitemgroup

5. Collision Detection

Each item has a local coordinate system, which can be selected through this coordinate system, moving, and so on, the transform () function can be easily implemented by the control matrix.

There is a very NB feature is the item support collision detection, how to detect it, first need to define for each type of item its bounding box, the same as the Boundingrect () function can set this item peripheral rectangle bounding box, and more precisely with shape () The function defines the path to the outer border of the item, which is Qpainterpath, which can be a direct, Bezier curve, any line, Collideswith () is a virtual function that can complete the collision detection function once the definition is complete.

Other Graphics Classes

Qgraphicsanchorlayout is the layout used to arrange several widgets in the form of anchor points.

Qgraphicseffect offers a number of graphic effects.

The main 4 standard effects are as follows:

Qt provides the following standardeffects:

· Qgraphicsblureffect-blurs the item by a given radius

· Qgraphicsdropshadoweffect-renders a dropshadow behind the item

· Qgraphicscolorizeeffect-renders the item in shades of any given color

· Qgraphicsopacityeffect-renders the item with an opacity

Graphics View coordinate system

The coordinate system is represented by (x, y), and one unit is a pixel on the screen. Within this framework there are 3 coordinate systems, item local coordinate system, scene coordinate system, view coordinate system, and functions used to map between these coordinate systems.

Item coordinates

The coordinates of item are built around its center point (0,0), which is also the center of all deformations, often constructed by points, lines, and rectangles in the local coordinate system.

When building a custom item, you just need to take local coordinates into account, and Qgraphicsview and Qgraphicsscene will do all the other transformations. When a mouse event is used for item, the point is automatically converted into the item coordinate system. The bounding rect and shape of item are defined in the local coordinate system.

The center position of the child item is saved to the parent item's coordinate system for automatic lookup, and the center coordinate of the topmost item is saved in the scene coordinate system.

The rotation scale operation of the parent item will affect the child item to follow together, but will not affect the coordinate system of the child item with the parent item, for example, there is a parent child 2 item, in the parent coordinate system, the child's position in (10,0). Thus, the coordinates of the point in the sub-coordinate system (0,10) in the parent coordinate system are (10,10). I then rotated and scaled the parent item, and the point under the sub-coordinate system (0,10) is still (10,10) in the parent coordinate system, but it changes in the scene coordinate system, and the child item is deformed with the parent, and if the parent is scaled to (2x,2x), in the scene coordinate system , the position of the child is (20,0), and the point in the sub-coordinate system (10,0) is in the coordinates of the scene (40,0). Qgraphicsitem::pos () is a few exceptions, which returns the item's coordinates in the parent coordinate system.

The rotation and scaling operations are fairly straightforward, and the ratate () and scale () functions are called in Qgraphicsview.

————————————————————————————————————————————————————————

If you say you don't practice your mouth bashi, you must make a test project.

Graphicwidget inherits from Qgraphicsview, overloading a function, Drawbackground ().

Ellipseitem inherits from Qgraphicsitem and overloads several functions:

QRECTF boundingrect () const;
void Paint (Qpainter *painter, const qstyleoptiongraphicsitem*option, Qwidget *widget);
Protected:
qvariant itemChange (graphicsitemchange change, const qvariant &value);
void Mousepressevent (Qgraphicsscenemouseevent *event);
void Mousereleaseevent (Qgraphicsscenemouseevent *event);

Boundingrect must be overloaded, otherwise the compiler says this is an abstract class and cannot be instantiated.

Call Qgraphicsitem::mousepressevent (event) in Mousepressevent (); It's all good and convenient for Shi.

Other similarities.

The parent node pointer is saved in the Ellipseitem.

In the Graphicwidget constructor, new scene, set, and then new a ellipseitem out, put into scene, specify the position, it is possible.

The result is that you can move the small ball that appears.

Cond...

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.