QT Graphics (Qpainterpath)

Source: Internet
Author: User
Tags extend in degrees
Brief Introduction

The Qpainterpath class (drawing path) provides a container for drawing operations that can create and reuse graphic shapes.

A drawing path is an object that consists of many graphical building blocks, such as rectangles, ellipses, lines, and curves. Building blocks can be added to enclosed sub-paths, such as rectangles or ellipses. The starting and ending points of closed paths are consistent, or they can exist independently as unclosed sub-paths, such as lines and curves.

Qpainterpath can be filled, outlined, cropped. To generate a filled outline for a specified drawing path, you can use the Qpainterpathstroker class. The main advantage of Qpainterpath compared to normal plotting is that complex graphics are created only once, and then simply called Qpainter: the:d rawpath () function can be drawn multiple times.

Qpainterpath provides a set of functions that you can use to get information about the drawing path and its elements. In addition to using the toreversed () function to change the order of elements, there are several functions that convert the Qpainterpath object to a polygon representation.

Brief introduction to create Qpainterpath Qpainterpath information Qpainterpath transform ellipse polygon Rectangle text arc Bezier fill rule

Create Qpainterpath

The Qpainterpath object can construct an empty path with a specified starting point, or a copy of another Qpainterpath object.

Once created, lines and curves can be added to the path using the LineTo (), ArcTo (), Cubicto (), and quadto () functions, and the lines and curves extend from the currentposition () to the point where they pass the parameters.

The CurrentPosition () of the Qpainterpath object is always the final position (or initial starting point) of the last child path added, using the MoveTo () function to move the Currentpositon without adding components (), MoveTo () The function implicitly starts a new subpath and closes the previous one. Another way to start a new subpath is to call the Closesubpath () function, which closes the current path by adding a line (from CurrentPosition () to the starting position). Note: The new path will be (0, 0) as its initial currentposition ().

Qpainterpath also provides some handy functions to add a closed subpath-addellipse (), Addpath (), Addrect (), Addregion (), and AddText (). The Addpolygon () function adds an unclosed subpath. In fact, these functions are a collection of MoveTo (), LineTo (), Cubicto () operations.

Also, use the Connectpath () function to add the path to the current path. Note, however, that the function will connect the last element of the current path to the given first element by adding a line. Qpainterpath Information

The Qpainterpath class provides a set of functions that return information about the path and its elements.

The currentposition () function returns the end point (or initial starting point) of the last sub-path that was added. The ElementAt () function can be used to retrieve various sub-path elements, and the Elementcount () function can be used to retrieve the number of elements, and the IsEmpty () function can tell the Qpainterpath whether the object contains any elements.

The Controlpointrect () function returns a rectangle that contains a bit and a control point in this path. This function is much faster than the exact boundingrect () that returns the bounding rectangle of this painter's path using floating-point precision.

Finally, Qpainterpath provides a contains () function that determines whether the given point or rectangle is within the path. and the intersects () function, which determines whether any point within a given rectangle is also within that path. Qpainterpath Conversion

For compatibility reasons, you may want to simplify the representation of the drawing path: the Tofillpolygon (), Tofillpolygons (), and tosubpathpolygons () functions provided by Qpainterpath are used to convert the drawing path to a polygon. Tofillpolygon () returns the drawing path as a single polygon, and the next two functions return a list of polygons.

The Tofillpolygons () and tosubpathpolygons () functions are provided, because drawing a few small polygons is usually faster than drawing a large polygon, even if the total number of points plotted is the same. The difference is the number of polygons they return: Tosubpathpolygons () creates a polygon for each subpath, regardless of the intersecting sub-paths (that is, overlapping bounding rectangles), and the Tofillpolygons () function creates only one polygon for the overlapping subpath.

The Tofillpolygon () and Tofillpolygons () functions first convert all the sub-paths to polygons, and then use the re-reel technique to ensure that overlapping sub-paths are populated with the correct fill rules. Note: the re-reel inserts additional lines into the polygon, so the outline of the filled polygon does not match the outline of the path. Ellipse

void Qpainterpath::addellipse (const QRECTF & Boundingrectangle)

Creates an ellipse within the specified Boundingrectangle and adds it as an enclosing subpath to the drawing path.

The ellipse consists of a clockwise curve, starting point and end point at 0° (3 o'clock).

Qlineargradient mygradient;
Qpen Mypen;
QRECTF Boundingrectangle;

Qpainterpath MyPath;
Mypath.addellipse (boundingrectangle);

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
Polygon

void Qpainterpath::addpolygon (const qpolygonf & Polygon)

Adds the specified polygon as a sub-path (not enclosed) to the drawing path.

Note: After adding polygon, the current position is the last point of polygon. To draw a line back to the starting point, use the Closesubpath () function.

Qlineargradient mygradient;
Qpen Mypen;
Qpolygonf MyPolygon;

Qpainterpath MyPath;
Mypath.addpolygon (MyPolygon);

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
Rectangle

void Qpainterpath::addrect (const QRECTF & Rectangle)

Adds the specified rectangle as a sub-path (enclosing) to the drawing path.

Rectangle as a clockwise set of lines is added. When you add rectangle, the current position of the drawing path is the upper-left corner of rectangle.

Qlineargradient mygradient;
Qpen Mypen;
QRECTF MyRectangle;

Qpainterpath MyPath;
Mypath.addrect (myrectangle);

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
text

void Qpainterpath::addtext (const qpointf & point, const Qfont & font, const QString & Text)

Adds the specified text to this path as a set of enclosing sub-paths created by the font. Position the subpath so that the left end of the text baseline is at the specified point.

Qlineargradient mygradient;
Qpen Mypen;
Qfont MyFont;
qpointf baseline (x, y);

Qpainterpath MyPath;
Mypath.addtext (Baseline, MyFont, tr ("Qt"));

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
Curved

void Qpainterpath::arcto (const QRECTF & Rectangle, qreal startangle, Qreal sweeplength)

Creates an arc that occupies the specified rectangle to specify startangle to start and to extend sweeplength degrees counterclockwise.

Angles are measured in degrees, and a negative angle can be used to specify a clockwise arc.

Note: if they are not already connected, this function connects the starting point of the arc to the current position. After the arc is added, the current position is the last point of the arc. To draw a line back to the starting point, use the Closesubpath () function.

Qlineargradient mygradient;
Qpen Mypen;

qpointf Center, StartPoint;

Qpainterpath MyPath;
Mypath.moveto (center);
Mypath.arcto (Boundingrect, startangle,
             sweeplength);

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
Bezier Curve

void Qpainterpath::cubicto (const QPOINTF & C1, const QPOINTF & c2, const qpointf & EndPoint)

Adds a Bezier curve between the current position and the specified endPoint using the specified control point, C1, C2.

When the curve is added, the current position is updated to the end of the curve.

Qlineargradient mygradient;
Qpen Mypen;

Qpainterpath MyPath;
Mypath.cubicto (c1, c2, endPoint);

qpainter painter (this);
Painter.setbrush (mygradient);
Painter.setpen (mypen);
Painter.drawpath (MyPath);
Fill Rule

QT provides two rules for populating a path:

Qt::oddevenfill (default) Qt::windingfill

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.