2D graph of Qt

Source: Internet
Author: User
Qt provides a powerful 2D drawing system that can be used to draw images on screens and drawing devices using the same API. It is mainly based on three classes: QPainter, QPaintDevice, and QPaintEngine. The QPainter class is used to perform drawing operations. The QPaintDevice class provides drawing devices (the QPaintDevice class is the base class of all objects that can be drawn. it

Qt provides a powerful 2D drawing system that can be used to draw images on screens and drawing devices using the same API. It is mainly based on three classes: QPainter, QPaintDevice, and QPaintEngine. The QPainter class is used to perform drawing operations. The QPaintDevice class provides drawing devices (the QPaintDevice class is the base class of all objects that can be drawn. it

Qt provides a powerful 2D drawing system that can be used to draw images on screens and drawing devices using the same API. It is mainly based on three classes: QPainter, QPaintDevice, and QPaintEngine. The QPainter class is used to perform drawing operations. The QPaintDevice class provides drawing devices (the QPaintDevice class is the base class of all objects that can be drawn, its sub-classes mainly include QWidget, QPixmap, QPicture, QImage, and QPrinter. It is a two-dimensional space abstraction and can be drawn using QPainter. The QPaintEngine class provides some interfaces, it can be used to draw QPainter on different devices.


In the drawing system, QPainter is used to complete specific painting operations, and provides a large number of highly optimized functions to complete most of the painting required for GUI programming. QPainter can draw on any object inherited from the QPaintDevice class. Key points: QPainter is generally drawn in the paintEvent () processing function of a Paint Event. First, you must create a QPainter object, then draw the image, and finally destroy the QPainter object.

Common graphic rendering functions provided by QPainter


The instance code is as follows:

# Include "widget. h" # include "ui_widget.h" # include
 
  
Widget: Widget (QWidget * parent): QWidget (parent), ui (new Ui: Widget) {ui-> setupUi (this);} Widget ::~ Widget () {delete ui;} void Widget: paintEvent (QPaintEvent * event) {QPainter painter (this); // draw a line painter. drawLine (QPoint (0, 0), QPoint (100,100); // create a paint brush QPen pen (Qt: green, 5, Qt: DotLine, Qt: RoundCap, qt: RoundJoin); // use the paint brush painter. setPen (pen); QRectF rectangle (70.0, 40.0, 80.0, 60.0); int startAngle = 30*16; int spanAngle = 120*16; // draw an arc painter. drawArc (rectangle, startAngle, spanAngle );/ /Reset the pen. setWidth (1); pen. setStyle (Qt: SolidLine); painter. setPen (pen); // draw a rectangle painter. drawRect (160, 20, 50, 40); // create a paint brush QBrush brush (QColor (0, 0,255), Qt: Dense4Pattern); // use a paint brush painter. setBrush (brush); // draw an elliptical painter. drawEllipse (220, 20, 50, 50); // you can specify a texture for a brush. setTexture (QPixmap (".. /yafeilinux.png "); // use painter again. setBrush (brush); // defines four points static const QPointF points [4] = {QPointF (270.0, 80.0), QPointF (290.0, 10.0), QPointF (350.0, 30.0), QPointF (390.0, 70.0)}; // draw a polygon painter using four points. drawPolygon (points, 4); // fill the painter in a rectangular area with a paint brush. fillRect (QRect (10,100,150, 20), QBrush (Qt: darkYellow); // erase the content painter in a rectangle. eraseRect (QRect (50, 0, 50,120); // linear gradient QLinearGradient linearGradient (QPointF (40,190), QPointF (70,190); // Insert the color linearGradient. setColorAt (0, Qt: yellow); linearGradient. s EtColorAt (0.5, Qt: red); linearGradient. setColorAt (1, Qt: green); // specify the spread mode of the area outside the gradient area linearGradient. setSpread (QGradient: RepeatSpread); // use the gradient as the painter for painting. setBrush (linearGradient); painter. drawRect (10,170, 90, 40); // radiation gradient QRadialGradient radialGradient (QPointF (200,190), 50, QPointF (275,200); radialGradient. setColorAt (0, QColor (255,255,100,150); radialGradient. setColorAt (1, QColor (0, 0, 0, 50); painter. setBrush (radialGradient); painter. drawEllipse (QPointF (200,190), 50, 50); // The QConicalGradient conicalGradient (QPointF (350,190), 60); conicalGradient. setColorAt (0.2, Qt: cyan); conicalGradient. setColorAt (0.9, Qt: black); painter. setBrush (conicalGradient); painter. drawEllipse (QPointF (350,190), 50, 50); // the paint brush uses a linear gradient to draw straight lines and text painter. setPen (QPen (linearGradient, 2); painter. drawLine (0, 280,100,280); painter. drawText (150,280, tr ("helloQt! "));}
 
Objects created using the QPainter: QPainter (QPaintDevice * device) constructor will be drawn on the device immediately, and the begin () function is automatically called, then, call the end () function in the destructor of QPainter to end the painting.


Gradient fill: In Qt, The QGradient class is used to develop gradient fill with QBrush.

1. The color of the linear gradient is inserted between the start point and the end point;

2. Color is inserted between the focus and the ring around the radiation gradient;

3. Color is inserted around the center of the conical gradient;

The three-minute gradient is represented by three sub-classes of QGradient: QLinearQradient indicates linear gradient, QRadialGradient indicates radiation gradient and QConicalGradient indicates conical gradient.


Anti-sawtooth Rendering

You can use the QPainter: setRenderHint () function rendering prompt to specify whether to use the ripple-toothed function. The function is mainly to smooth the image edge, make it look softer and smoother.


Coordinate Transformation

The ing between the logical coordinates of QPainter and the physical coordinates of the drawing device (the default Coordinate System of the drawing device is (0, 0) is handled by the transformation matrix, the viewport, and the window of QPainter, logical and physical coordinates are the same by default. You can use the QPainter: scale () function to scale the coordinate system. Use the QPainter: rotate () function to rotate the coordinate system clockwise. Use the QPainter: translate () function to translate the coordinate system; use QPainter: shear () to distort the coordinate system around the origin.

1. Basic Transformation

The 2D transformation of the coordinate system is implemented by the QTransform class, And the QTransform class object can store multiple transformation operations. We recommend that you use the QTransform class object when the same transformation is used for multiple times. The transformation of the coordinate system is realized through the transformation matrix, and one point to another point can be transformed on the plane. All transformation matrices can be obtained using the QPainter: worldTransform () function. To set a transformation matrix, you can use the QPainter: setWorldTransform () function, these two functions can also be replaced by the QPainter: transform () and QPainter: setTransform () functions.

During the transformation operation, the coordinate system may need to be changed multiple times and then restored, Which is messy and prone to Operation errors. In this case, you can use the QPainter: save () function to save the transformation matrix of QPainter. It will save the transformation matrix to an internal stack, and then use QPainter to restore the transformation matrix:: The restore () function pops up.


2. Window-view Conversion

When you use QPainter to plot, logical coordinates are used and then converted to physical coordinates of the drawing device. The ing between logical coordinates and physical coordinates is handled by the worldTransform () function of QPainter, viewport () and window () function of QPainter. In the preceding example, a window represents an arbitrary rectangle under physical coordinates, while a window represents the same rectangle under logical coordinates. By default, the logical coordinates and the Interior coordinates overlap, which are equivalent to the rectangles on the drawing device. A good solution is to maintain the same aspect ratio for the views and windows to Prevent Deformation:

Int side = qMin (width (), height (); int x = (width ()/2); int y = (height ()/2 ); // set the window-the painter for converting the view. setViewport (x, y, side, side );

Window-the window conversion is only a linear transformation and does not perform the cropping operation. This means that if the painting range is beyond the current window, the same linear algebra method will still be used to transform the painting to the viewport.

# Include "widget. h" # include "ui_widget.h" # include
 
  
Widget: Widget (QWidget * parent): QWidget (parent), ui (new Ui: Widget) {ui-> setupUi (this);} Widget ::~ Widget () {delete ui;} void Widget: paintEvent (QPaintEvent * event) {QPainter painter (this); // enter the background of the white painter. fillRect (rect (), Qt: white); painter. setPen (QPen (Qt: red, 11); // draw a painter line segment. drawLine (QPoint (5, 6), QPoint (100, 99); // translate the coordinate system so that (200,150) points are used as the origin painter. translate (200,150); // enable anti-sawtooth painter. setRenderHint (QPainter: Antialiasing); // re-draw the painter of the same line segment. drawLine (QPoint (5, 6), QPoint (100, 99); // Save the painter status painter. save (); // rotate the Coordinate System 90 degrees painter. rotate (90); painter. setPen (Qt: cyan); // re-draw the same line segment painter. drawLine (QPoint (5, 6), QPoint (100, 99); // restore painter status painter. restore (); painter. setBrush (Qt: darkGreen); // draw a rectangle painter. drawRect (-50,-50,100, 50); painter. save (); // scale the painter in the coordinate system. scale (0.5, 0.4); painter. setBrush (Qt: yellow); // re-draw the same rectangle painter. drawRect (-50,-50,100, 50); painter. restore (); painter. setPen (Qt: blue); painter. setBrush (Qt: darkYellow); // draw an elliptical painter. drawEllipse (QRect (60,-100, 50, 50); // twist the coordinate system into a painter. shear (1.5,-0.7); painter. setBrush (Qt: darkGray); // re-draw the same elliptical painter. drawEllipse (QRect (60,-100, 50, 50 ));}
 
#include "widget.h"#include "ui_widget.h"#include 
 
  #include 
  
   #include 
   
    #include 
    
     Widget::Widget(QWidget *parent) :    QWidget(parent),    ui(new Ui::Widget){    ui->setupUi(this);    setMouseTracking(true);    QTimer *timer = new QTimer(this);    connect(timer, SIGNAL(timeout()), this, SLOT(update()));    timer->start(1000);    angle = 0;}Widget::~Widget(){    delete ui;}void Widget::paintEvent(QPaintEvent *event){    angle += 10;    if(angle == 360)        angle = 0;    int side = qMin(width(), height());    QPainter painter(this);    painter.setRenderHint(QPainter::Antialiasing);    QTransform transform;    transform.translate(width()/2, height()/2);    transform.scale(side/300.0, side/300.0);    transform.rotate(angle);    painter.setWorldTransform(transform);    painter.drawEllipse(-120, -120, 240, 240);    painter.drawLine(0, 0, 100, 0);}void Widget::mouseMoveEvent(QMouseEvent *event){    QString pos = QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());    QToolTip::showText(event->globalPos(), pos, this);}
    
   
  
 

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.