当前位置:网站首页>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 .
边栏推荐
- 我的富二代朋友
- 【分布式开发】之 CAP 原则
- 彻底搞懂kubernetes调度框架与插件
- Cap principle of [distributed development]
- Dart code comments and documentation specification
- Oxygen temperature and humidity module
- Standing at the crossroads of digital retail transformation, we need to look at it from a new perspective
- JUC concurrent programming learning
- C#学习笔记----C#连接MySQL数据库
- Let's move forward together, the 10th anniversary of Google play!
猜你喜欢

Leetcode 2347. the best poker hand

Lua get started quickly

Data problems can also be found if there is a space at the end of the field value of MySQL query criteria

Codeforces summer training weekly (7.14~7.20)

Shutter -- password login registration interface

Codeforces暑期训练周报(7.14~7.20)

JUC concurrent programming learning

Article reproduction: super resolution network fsrcnn

路由策略简介

伦敦银开盘时间知多少
随机推荐
Fluent call interface UI
文章复现:超分辨率网络FSRCNN
LeetCode 2341. 数组能形成多少数对
“你“想当测试/开发程序员吗?努力发芽的我们......
If you are still using WiFi, you will be out: li-fi is better!!!
Software test interview question: how to prepare test data? How to prevent data pollution?
Knowledge of two-dimensional array
The understanding of domain adaptation in transfer learning and the introduction of three technologies
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
Unknown database ‘xxxxx‘
Codeforces summer training weekly (7.21~7.27)
Spreadsheet export excel table
I want to get 20K after 3 years of experience, but I haven't got it for half a month?
Sort out SQL performance optimization and collect classics!
Thoroughly understand kubernetes scheduling framework and plug-ins
Qlib教程——基于源码(二)本地数据保存与加载
[C language] file operation
Dart code comments and documentation specification
My rich second generation friend
Software test interview question: how to find problems related to the database?