Drawing Drawing Qt substantially --QPainter

Qt QPainter QPaintDevice drawing system based on classes and QPaintEngine

1) QPainter:. Classes for drawing operation

2) .QPaintDevice: QPainter can be used for drawing a two-dimensional abstract interface

3) .QPaintEngine: providing QPainter drawing on different interface used by QPainter and internal QPainterDevice, applications need to call QPaintEngine, unless you want to create your own device type

 The drawing device generally comprises: QWidget, QPixmap, QImage the like, which drawing device to provide a "canvas" of QPainter

 

1. QWidget and its subclasses are the most common drawing device class inherits from QWidget have QPaintEvent event to the device the drawing , only override this event , and write response code

 ::paintEvent(QPaintEvent *e)

{

  QPainter Painter (the this) // associated with the drawing device to draw the object QPainter

  // painter drawing device on the window

    

}

 

2. The main properties of the drawing QPainter

QPainter drawing, the main draw some basic graphic elements (points, lines, circles, rectangles, curves, text), the control elements of the characteristic of these plots QPainter three main properties:

  1) pen property: is a QPen object for controlling the line color, width, line type

  2) brush properties: the object is a QBrush, filling property for a region may be provided fill color, fill mode, gradation characteristics

  3) font attributes: the object is a QFont, when used for drawing color is provided a font style, size property

Using three basic control properties of the basic characteristics of the drawing, there are other features can be combined: superimposing rotation and scaling

 

3. QPen used when drawing the line set, including the width, color, line type, the main interface function QPen class, typically corresponding to a set function has a function of reading

setColor (QColor & color) // Set the pen color line color

setWidth (int width) // set the line width

setStyle (Qt :: PenStyle style) // set the line style, the parameter is an enumeration type Qt :: PenStyle

setCapStyle (Qt :: PenCapStyle style) // set the line cap style

setJoinStyle (Qt :: PenJoinStyle style) // set the connection style

 

4. QBrush filling characteristics for drawing: fill style fill color, fill material when the material of pictures

setColor (QColor & color) // set the color brush, that is filled with solid fill color

setStyle (Qt :: BrushStyle style) // set the brush style

setTexture (QPixmap & pixmap) // set a QPixmap type of picture as a brush picture brush style is automatically set to Qt :: TexturePattern

setTextureImage (QImage & image) // set a QImage type of picture as a brush picture brush style is automatically set to Qt :: TexturePattern

 

setStyle (Qt :: BrushStyle style) // set the brush style

Qt :: SolidPatton // filled with a single color

Qt :: HorPatton // fill the horizon

Qt :: VerPatton // vertical line fill

Qt :: TexturePattern // fill material needs to be specified texture or textureImage Pictures

Qt :: QLinearGradient // linear gradient

Qt :: QRadialGradient // radial gradient

Qt :: QConicalGradient // cone gradient

. 1 QPixmap texturePixmap ( " : Images / Images / texture.jpg " );
 2  QBrush Brush
 . 3 brush.setStyle (the Qt :: TexturePattern)           // brush fill materials 
. 4 brush.setTexture (texturePixmap)              // set the material image 
. 5 Painter. setBrush (brush)

Gradient fill of 5 .QBrush

3 to achieve a gradual filling of the classes:

1) QLinearGradient // linear gradient starting point to develop a color and an end color and can also specify a point intermediate color, the color linear interpolation between the start and end points is calculated

2) QRadialGradient // simple radiation and radiation expansion

3) QConicalGradient // conical gradient

Also you need to use a method setSpread (QGradient :: Spread method) extended function setting mode, without filling the conical extension effect

PadSpread filling the outer region (default) color end point

Filling the outer region RepeatSpread reuse easy way

Filling the outer region ReflectSpread reflective reuse easy way

. 1 #include " paintt.h " 
2 #include " ui_paintt.h " 
. 3 #include <QPaintEvent>
 . 4 #include <QPainter>
 . 5  
. 6  
. 7 PaintT :: PaintT (the QWidget * parent):
 . 8      the QWidget (parent),
 . 9      UI ( new new Ui :: PaintT)
 10  {
 . 11      ui-> setupUi ( the this );
 12 is      setPalette (QPalette (the Qt :: white)); // control white background palette setting window 
13 is      setAutoFillBackground ( to true );
 14  }
 15 
16 PaintT::~PaintT()
17 {
18     delete ui;
19 }
20 
21 void PaintT::paintEvent(QPaintEvent *event)
22 {
23     QPainter painter(this);
24     painter.setRenderHint(QPainter::Antialiasing);   //线条抗锯齿
25     painter.setRenderHint(QPainter::TextAntialiasing);
26 
27     int w = this->width();             //绘图区宽度
28     int h = this->height();            // drawing area height 
29  #if 0
 30      QRect RECT (W / . 4 , H / . 4 , W / 2 , H / 2 );       // middle region with the rectangle rectangular frame size variation varies widget 
31 is      QPen PEN;
 32      pen.setWidth ( . 3 );
 33 is      pen.setColor (the Qt :: Red);
 34 is      pen.setStyle (the Qt :: DashDotLine);
 35      pen.setCapStyle (the Qt :: flatcap);
 36      pen.setJoinStyle (the Qt :: BevelJoin) ;
 37 [      painter.setPen (PEN);
 38 is  
39      QBrush Brush;
 40      brush.setColor (the Qt :: Yellow);
 41 is     brush.setStyle (the Qt :: SolidPattern);
 42 is      painter.setBrush (Brush);
 43 is  #endif 
44 is  // QRadialGradient :: QRadialGradient (QREAL CX, CY QREAL, QREAL RADIUS, QREAL FX, FY QREAL) 
45      QRadialGradient radialGrad (W / 2 , H / 2 , Qmax (W / . 8 , H / . 8 ), W / 2 , H / 2 ); // radiation filling the center point, the radius of the radiation to filling, the focus coordinates 
46 is      radialGrad.setColorAt ( 0 , the Qt :: Green);    // used herein, the logical coordinates 
47      radialGrad.setColorAt ( a , the Qt :: Blue);     // 0 origin: a center point of the end of irradiation: filled region circumferential
48      radialGrad.setSpread (QGradient :: ReflectSpread);
 49      painter.setBrush (radialGrad);
 50  
51 is      painter.drawRect ( the this -> RECT ());    // i.e. widget entire rectangle window filled radiation than the defined region extending effect 
52 }

Hypnosis comes with vertigo effect, constantly drawn to refresh, do not know will not appear dynamic effects

 

6.QPainter drawPath plotted as a function of a composite graphical object

 

Guess you like

Origin www.cnblogs.com/AmyBKLP/p/11703564.html