QT Graphics (basic drawing of Qpainter)

Source: Internet
Author: User
Tags drawtext

Briefly

QT provides a powerful 2D drawing system that can be drawn on screen and drawing devices using the same API, which is based primarily on the three classes of Qpainter, Qpaintdevice, and Qpaintengine.

    • Qpainter is used to perform drawing operations, which provide APIs for displaying graphics (lines, shapes, gradients, and so on), text, and images in the GUI or qimage, Qopenglpaintdevice, Qwidget, and Qpaintdevice.

    • Qpaintdevice does not directly draw the physical display screen, but uses the intermediary medium of the logical interface. For example, when drawing a rectangular shape, you must use qpicture in order to draw objects into the middle of qwidget, Qglpixelbuffer, Qimage, Qpixmap, Qpaintdevice, and more.

    • Qpaintengine provides a number of interfaces that can be used for qpainter to draw on different devices.

The drawing system is completed by Qpainter, and the Qpainter class provides a number of highly optimized functions to complete most of the drawing work required for GUI programming. It can draw all the shapes you want, from the simplest straight line to any other complex graphic, such as pie charts, arcs, etc., and can be used to draw text and pictures.

Qpainter can draw operations on any object that inherits from the Qpaintdevice class.

Qpainter is generally drawn in the drawing event PaintEvent () of the part, first creating the Qpainter object, then drawing the graphic, and finally remembering to destroy the Qpainter object. This member function is called when a window program needs to be upgraded or redrawn. After using repaint () and update (), the function paintevent () is called.

    • Briefly
    • Draw text
      • Effect
      • Source
    • Draw a line
      • Effect
      • Source
    • Draw a rectangle
      • Effect
      • Source
    • Draw an arc
      • Effect
      • Source
    • Draw Ellipse
      • Effect
      • Source
    • Drawing polygons
      • Effect
      • Source
    • Drawing pictures
      • Effect
      • Source
    • Summarize

The following is a simple example of how member functions PaintEvent () are used.

Draw text Effects

Source
void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Qpainterpainter (This);//Set Brush colorPainter. Setpen(Qcolor (0, .) );//Set font: Microsoft ya black, dot size 50, italicQfontFont;Font. setfamily("Microsoft Yahei");Font. Setpointsize(50);Font. Setitalic(true); Painter. SetFont(Font);//Draw textPainter. DrawText(rect ()Qt:: AlignCenter, "Qt");}

First, a Qpainter object is created for the part, which is used for subsequent drawing. Use Setpen () to set the color of the brush (light blue). By using Qfont to build the font we want, setfamily () sets the font for Microsoft Jas Black, Setpointsize () set the point size 30, setitalic () to set the italic, and then sets the font through SetFont (), and finally calls DrawText () To achieve text rendering, where Rect () refers to the current form's display area, qt::aligncenter refers to the text centered drawing.

Draw a line effect

Source
void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Qpainterpainter (This);//anti-aliasingPainter. Setrenderhint(Qpainter:: antialiasing, true);    //Set Brush colorPainter. Setpen(Qcolor (0, .) );//Draw a linePainter. DrawLine(qpointf (0, height ()),qpointf (width () / 2, height () / 2));}

First we set the anti-aliasing through Setrenderhint (), or the drawn lines appear jagged, and call Setpen () to set the brush color (light blue). Finally call DrawLine () to achieve the drawing of the line, where qpointf (0, height ()) refers to the line's starting coordinates, qpointf (width ()/2, height ()/2) refers to the end coordinate of the line.

Draw a rectangle effect

Source
void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Qpainterpainter (This);//anti-aliasingPainter. Setrenderhint(Qpainter:: antialiasing, true);    //Set brush color, WidthPainter. Setpen(Qpen (qcolor (0,   2));//Set paint brush colorPainter. Setbrush(Qcolor (255, ,) ); Painter. DrawRect(50, 50, 160, 100);}

First we use Setpen () to set the brush color (light blue), width (2 pixels), to set the border of the rectangle area. Then use Setbrush () to set the brush color (orange) to fill the rectangular area, and finally call DrawRect () to achieve the rectangle's drawing, where the parameters in order of x, Y, W, H, refers to the area from X to 50,y to 50 of the coordinate point, the width of 160, A rectangle with a height of 100.

Draw an arc effect

Source
voidMainWindow::p aintevent (Qpaintevent *Event) {q_unused (Event);//RectangleQRECTF Rect (90.0,90.0,80.0,90.0);//Starting angle    intStartAngle = -* -;//Across degrees    intSpanangle = -* -; qpainter painter ( This);//anti-aliasingPainter.setrenderhint (Qpainter::antialiasing,true);//Set brush color, WidthPainter.setpen (Qpen (Qcolor (0, the, the),2));//Draw arcsPainter.drawarc (Rect, startangle, spanangle);}

When you draw an arc, the angle is divided into one-sixteenth, that is, if you want 30 degrees, you need to be 30*16. It has a starting angle and span, and a position rectangle, so if you want to draw the arc you want, you need to approximate the estimate of each parameter.

Draw an Ellipse effect

Source
void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Qpainterpainter (This);//anti-aliasingPainter. Setrenderhint(Qpainter:: antialiasing, true);    //Set brush color, WidthPainter. Setpen(Qpen (qcolor (0,   2));//Draw EllipsePainter. DrawEllipse(qpointf (+) , 50, 20);//Set paint brush colorPainter. Setbrush(Qcolor (255, ,) );//Draw a circlePainter. DrawEllipse(qpointf (+) , 40, 40);}

Here we draw an ellipse and a circle, are called DrawEllipse interface, we can easily find that, if the ellipse, the following two parameters are different, the circle is the same. First, let's take a look at the first parameter qpointf refers to the position of the center point of the ellipse relative to the current form qpoint (0, 0), followed by the x-axis of the ellipse and the radius of the y-axis.

Draw Polygon Effects

Source
voidMainWindow::p aintevent (Qpaintevent *Event) {q_unused (Event); qpainter painter ( This);//anti-aliasingPainter.setrenderhint (Qpainter::antialiasing,true);//Set Brush colorPainter.setpen (Qcolor (0, the, the));//coordinates of each point    Static Constqpointf points[4] = {qpointf ( -, +), qpointf ( -, Max), qpointf ( Max, the), qpointf ( -, -)};//Draw polygonsPainter.drawpolygon (points,4);}

First of all we have the position of the coordinates point, here are four points, respectively: qpointf (30, 40), qpointf (60, 150), qpointf (150, 160), qpointf (220, 100), Then call DrawPolygon to connect the dots and draw them as polygons.

Draw a picture effect

Source
void MainWindow::paintEvent(QPaintEvent *event){    Q_UNUSED(event);    QPainter painter(this);    // 反走样    true);    // 绘制图标    painter.drawPixmap(rect(), QPixmap(":/Images/logo"));}

by Drawpixmap () to draw the picture, we can specify the area of the picture drawing Qrect, here is the whole area of the interface, when the interface stretching, the picture will also follow the extension.

Summarize

Basic text, lines, rectangles, ellipses, polygons, pictures of the drawing has been shared, there are some details we did not explain, about the other graphics are similar to the drawing, we all in the back to do a detailed explanation.

QT Graphics (basic drawing of Qpainter)

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.