当前位置:网站首页>Brushes and brushes
Brushes and brushes
2022-07-28 01:39:00 【PHP code】
We mentioned in the previous chapter ,Qt The drawing system defines two key attributes used in drawing : Brushes and brushes . The former uses QBrush describe , Mostly used for filling ; The latter uses QPen describe , It is mostly used to draw contour lines .
QBrush Defined QPainter Fill patterns for , With style 、 Color 、 Gradient, texture and other attributes .
Paintbrush style() Defines the fill pattern , Use Qt::BrushStyle enumeration , The default value is Qt::NoBrush, That is, no filling . We can see the differences of various fill patterns from the following illustration :

Paintbrush color() Defines the color of the fill pattern . This color can be Qt Predefined color constants , That is to say Qt::GlobalColor, It can also be any QColor object .
Paintbrush gradient() Defines gradient fill . This attribute is only available when the style is Qt::LinearGradientPattern、Qt::RadialGradientPattern perhaps Qt::ConicalGradientPattern It is effective only after one . Gradients can be made by QGradient Objects represent .Qt Three gradients are provided :QLinearGradient、QConicalGradient and QRadialGradient, They are all QGradient Subclasses of . We can use the following code snippet to define a gradient brush :
QRadialGradient gradient(50, 50, 50, 50, 50);
gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));
QBrush brush(gradient);
When the brush style is Qt::TexturePattern when ,texture() Defines the texture used for filling . Be careful , Even if you don't set the style to Qt::TexturePattern, When you call setTexture() Function time ,QBrush Will automatically style() Set to Qt::TexturePattern.
QPen Defined for QPainter How to draw a line or outline . Brushes have styles 、 Width 、 A brush 、 Attributes such as cap style and connection style . The style of the brush style() Defines the style of the line . A brush brush() Used to fill the lines drawn by the brush . Cap style capStyle() Use defined QPainter The end of the drawn line ; Connection style joinStyle() Defines how the two lines are connected . The width of the brush width() or widthF() Defines the width of the brush . Be careful , Does not exist with a width of 0 Line . Suppose you set width by 0,QPainter A line will still be drawn , And the width of this line is 1 Pixels . in other words , The brush width is usually at least 1 Pixels .
So many parameters can be specified at construction time , You can also use set The function specifies , It all depends on your habits , for example :
QPainter painter(this);
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
Equivalent to
QPainter painter(this);
QPen pen; // creates a default pen
pen.setStyle(Qt::DashDotLine);
pen.setWidth(3);
pen.setBrush(Qt::green);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
painter.setPen(pen);
The advantage of using constructors is that the code is shorter , But the meaning of parameters is not clear ; Use set Function is just the opposite .
The default brush attribute is pure black ,0 Pixels , Square cap (Qt::SquareCap), Inclined connection (Qt::BevelJoin).
You can also use setDashPattern() Function custom style , For example, the following code snippet :
QPen pen;
QVector<qreal> dashes;
qreal space = 4;
dashes << 1 << space << 3 << space << 9 << space
<< 27 << space << 9 << space;
pen.setDashPattern(dashes);
The cap defines the style of the end of the brush , for example :

The difference between them is ,Qt::SquareCap It is a square endpoint containing the last point , Use half the lineweight to cover ;Qt::FlatCap The last point is not included ;Qt::RoundCap Is the end of a circle containing the last point . For details, please refer to the following example ( come from 《C++ GUI Programming with Qt 4, 2nd Edition》):

Again , You can refer to the following illustration to understand the details of these connection styles ( come from 《C++ GUI Programming with Qt 4, 2nd Edition》):

Be careful , We said before ,QPainter It's also a state machine , All the properties we mentioned here are in this state machine , therefore , We should remember whether to save it or rebuild it .
边栏推荐
- 软件测试面试题:如何发现数据库的相关问题?
- Leetcode 2341. How many pairs can an array form
- 自定义事件
- 8000字讲透OBSA原理与应用实践
- 彻底搞懂kubernetes调度框架与插件
- [C language] file operation
- Rviz uses arbotix to control robot motion
- QT setting Icon
- LeetCode 2341. 数组能形成多少数对
- In April, global smartphone shipments fell 41% year-on-year, and Huawei surpassed Samsung to become the world's first for the first time
猜你喜欢
随机推荐
JG data reset (WD)
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
Software process that testers need to know
面试题 01.08. 零矩阵
Software testing interview question: where do your performance testing requirements come from?
Gazebo control example
普通设备能不能接入TSN时间敏感网络?
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
MySQL进阶--存储过程以及自定义函数
迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
Shutter -- password login registration interface
Summary of common shortcut keys in idea
Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"
Spreadsheet export excel table
Opengauss active / standby architecture works with keeplive
字节月薪28K,分享一波我的自动化测试经验....
LeetCode 2347. 最好的扑克手牌
Software test interview question: think_ What is the function of time?
JG-数据重置(wd)


![[style set 1] tab](/img/b4/01575a37fcc69a7b4e39cade08413d.png)






