QPainter -- paint brush (QPen) and paint brush (QBrush), qpainter -- qpen

Source: Internet
Author: User

QPainter -- paint brush (QPen) and paint brush (QBrush), qpainter -- qpen

Article Reprinted from: https://www.devbean.net/2012/11/qt-study-road-2-brush-pen/

 

As mentioned in the previous chapter, the Qt drawing system defines two key attributes used for painting: paint brush and paint brush. The former usesQBrushDescription, mostly used for filling; the latter is usedQPenDescription, mostly used to draw outlines.

 

QBrushDefinedQPainterFill mode, with style, color, gradient, texture and other attributes.

Painter'sstyle()Defines the fill style, useQt::BrushStyleEnumeration. The default value isQt::NoBrush, That is, no filling is performed. The following figure shows the differences between various fill styles:

 

Painter'scolor()Defines the color of the fill mode. This color can be a color constant predefined by Qt, that isQt::GlobalColorAnd can be anyQColorObject.

Painter'sgradient()Gradient fill is defined. This attribute is available only when the style isQt::LinearGradientPattern,Qt::RadialGradientPatternOrQt::ConicalGradientPatternOnly valid at the moment. GradientQGradientObject Representation. Qt provides three gradient types:QLinearGradient,QConicalGradientAndQRadialGradient, They are allQGradient. We can use the following code snippet to define a gradient Paint Brush:

1 QRadialGradient gradient(50, 50, 50, 50, 50);2 gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));3 gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));4  5 QBrush brush(gradient);

When the image style isQt::TexturePattern,texture()Defines the texture used for filling. Note that even if you do not set the styleQt::TexturePatternWhen you callsetTexture()Function time,QBrushWill automaticallystyle()SetQt::TexturePattern.

QPenDefinedQPainterHow to draw lines or outlines. The paint brush has attributes such as style, width, paint brush, pen and cap style, and connection style. Paint Brush Stylestyle()Defines the line style. Paint Brushbrush()Fill in the lines drawn by the paint brush. Pen Cap StylecapStyle()Defined usageQPainterThe end of the drawn line; connection StylejoinStyle()Then it defines how the two lines are connected. Paint widthwidth()OrwidthF()Defines the width of the paint brush. Note that there is no line with a width of 0. Suppose you set the width to 0,QPainterA line is still drawn, and the width of the line is 1 pixel. That is to say, the width of the paint brush is usually at least 1 pixel.

These parameters can be specified either during construction or by using the set function, depending on your habits. For example:

1 QPainter painter(this);2 QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);3 painter.setPen(pen);

Equivalent

 1 QPainter painter(this); 2 QPen pen;  // creates a default pen 3   4 pen.setStyle(Qt::DashDotLine); 5 pen.setWidth(3); 6 pen.setBrush(Qt::green); 7 pen.setCapStyle(Qt::RoundCap); 8 pen.setJoinStyle(Qt::RoundJoin); 9  10 painter.setPen(pen);

 

The advantage of using constructor is that the Code is short, but the parameter meaning is not clear; using the set function is exactly the opposite.

The default paint brush property is pure black, 0 pixels, square pen cap (Qt::SquareCap), Inclined connection (Qt::BevelJoin).

The following is an example of a paint brush style:

 

You can also usesetDashPattern()Function custom styles, such as the following code snippet:

1  QPen pen;2  QVector<qreal> dashes;3  qreal space = 4;4  5  dashes << 1 << space << 3 << space << 9 << space6         << 27 << space << 9 << space;7  8  pen.setDashPattern(dashes);

 

The pen cap defines the style of the paint brush end, for example:

 

The difference between them is,Qt::SquareCapIs a square endpoint that contains the last vertex and uses half of the line width to overwrite it;Qt::FlatCapDoes not contain the last vertex;Qt::RoundCapIs the circular endpoint that contains the last vertex. For details, refer to the following example (from C ++ GUI Programming with Qt 4, 2nd Edition):

 

The connection style defines the style when two lines are connected, for example:

 

 

Similarly, you can refer to the following illustration to understand the details of these connection styles (from C ++ GUI Programming with Qt 4, 2nd Edition):

 

Note: As we mentioned earlier,QPainterIt is also a state machine. All the attributes we mentioned here are in this state machine. Therefore, we should remember whether to save them or rebuild them.

 

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.