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 usesQBrush
Description, mostly used for filling; the latter is usedQPen
Description, mostly used to draw outlines.
QBrush
DefinedQPainter
Fill mode, with style, color, gradient, texture and other attributes.
Painter'sstyle()
Defines the fill style, useQt::BrushStyle
Enumeration. 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::GlobalColor
And can be anyQColor
Object.
Painter'sgradient()
Gradient fill is defined. This attribute is available only when the style isQt::LinearGradientPattern
,Qt::RadialGradientPattern
OrQt::ConicalGradientPattern
Only valid at the moment. GradientQGradient
Object Representation. Qt provides three gradient types:QLinearGradient
,QConicalGradient
AndQRadialGradient
, 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::TexturePattern
When you callsetTexture()
Function time,QBrush
Will automaticallystyle()
SetQt::TexturePattern
.
QPen
DefinedQPainter
How 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 usageQPainter
The 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,QPainter
A 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::SquareCap
Is a square endpoint that contains the last vertex and uses half of the line width to overwrite it;Qt::FlatCap
Does not contain the last vertex;Qt::RoundCap
Is 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,QPainter
It 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.