当前位置:网站首页>Introduction to QT drawing system
Introduction to QT drawing system
2022-07-28 01:39:00 【PHP code】
Qt The drawing system allows the use of the same API Drawing on screens and other printing devices . The whole drawing system is based on QPainter,QPainterDevice and QPaintEngine Three categories .
QPainter Used to perform drawing operations ;QPaintDevice It's an abstraction of two-dimensional space , This two-dimensional space allows QPainter Draw on it , That is to say QPainter Working space ;QPaintEngine Provided a brush (QPainter) A unified interface for rendering on different devices .QPaintEngine Class applied to QPainter and QPaintDevice Between , Usually transparent to developers . Unless you need to customize a device , Otherwise you don't need to care QPaintEngine This class of . We can QPainter It's understood as a brush ; hold QPaintDevice The place where the brush is used , Like paper 、 Screen etc. ; And for paper 、 On screen , Be sure to use a different brush to draw , In order to unify the use of a brush , We designed QPaintEngine class , This class makes different papers 、 Every screen can use a brush .
The following figure shows the hierarchy between the three classes ( come from Qt API file ):

The diagram above tells us ,Qt Our drawing system is actually , Use QPainter stay QPainterDevice Drawing on , Use... Between them QPaintEngine To communicate ( That is translation QPainter Instructions ).
Let's introduce QPainter Use :
//!!! Qt4/Qt5
class PaintedWidget : public QWidget
{
Q_OBJECT
public:
PaintedWidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *);
};
Notice that we rewrote QWidget Of paintEvent() function . Maybe we are understanding Qt The first practical application after the event system . The next step is PaintedWidget Source code :
//!!! Qt4/Qt5
PaintedWidget::PaintedWidget(QWidget *parent) :
QWidget(parent)
{
resize(800, 600);
setWindowTitle(tr("Paint Demo"));
}
void PaintedWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawLine(80, 100, 650, 500);
painter.setPen(Qt::red);
painter.drawRect(10, 10, 100, 400);
painter.setPen(QPen(Qt::green, 5));
painter.setBrush(Qt::blue);
painter.drawEllipse(50, 150, 400, 200);
}
In the constructor , We just set the size and title of the window . and paintEvent() Functions are drawn code . First , We created a... On the stack QPainter object , in other words , Each run paintEvent() Function , Will rebuild this QPainter object . Be careful , This may give rise to some details : Because every time we rebuild QPainter, So the brush color set at the first run 、 State, etc , The second time you enter this function, you lose it all . Sometimes we want to keep the brush state , You have to save your own data , Otherwise, you need to QPainter As a member variable of a class .
QPainter Receive one QPaintDevice Pointer as parameter .QPaintDevice There are many subclasses , such as QImage, as well as QWidget. Remember ,QPaintDevice It can be understood as where to draw , And now we want to draw on this component , So what's coming in is this The pointer .
QPainter There's a lot to draw Initial function , Used for drawing all kinds of graphics , Like here drawLine(),drawRect() as well as drawEllipse() etc. . When drawing contours , Use QPainter Of pen() attribute . such as , We call painter.setPen(Qt::red) take pen Set to red , The rectangle drawn below has a red outline . Next , We will pen Change to green ,5 Pixel width (painter.setPen(QPen(Qt::green, 5))), Set the brush to blue again . Call again at this time draw function , It's green 5 Pixel wide contour 、 Ellipses filled in blue .
Run our program , You can see the final effect :

We will introduce the brush in detail in the following chapters QPen And brushes QBrush Properties of .
Another point to make , Please pay attention to our drawing order , First, straight lines , Then the rectangle , Finally, the ellipse . In this drawing order , You can see that the straight line is the first drawing , On the bottom floor ; Rectangle is the second drawing , On the middle floor ; Ellipse is the last drawing , At the top .
If you understand OpenGL, I must have heard such a sentence :OpenGL Is a state machine . The so-called state machine , That is to say ,OpenGL Only various states are saved . such as , Set the brush color to red , that , Unless you reset another color , Its color will always be red .QPainter In the same way , Its state will not recover itself , Unless you use various setting functions . therefore , If in the code above , We draw a rectangle after the ellipse , Its style will also be green 5 Pixel outline and blue fill , Unless you explicitly call the set function to update the status . This is how most drawing systems are implemented , Include OpenGL、QPainter as well as Java2D. It is because QPainter Is a state machine , Will lead to a detailed problem that we have previously introduced : because paintEvent() It requires repeated entry , therefore , Attention should be paid to the second entry ,QPainter Is your state consistent with the first time , Otherwise, it may cause flickering . This flicker is not due to the problem of double buffering , But because of the fast switching of the drawing state .
边栏推荐
- Spreadsheet export excel table
- Codeforces暑期训练周报(7.14~7.20)
- My rich second generation friend
- JG-数据重置(wd)
- ICML2022 | 在线决策Transformer
- 伦敦银开盘时间知多少
- URDF 集成 Gazebo
- Redefine analysis - release of eventbridge real-time event analysis platform
- Lua advanced
- Software test interview question: how to find problems related to the database?
猜你喜欢

Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"

字节月薪28K,分享一波我的自动化测试经验....

Icml2022 | online decision transformer

开发 Flutter 录音功能的插件

MATLAB 44种动漫渐变色绘图程序

Flutter 通话界面UI

【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)

Software process that testers need to know

Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine

ABAP CDs table function introduction and examples
随机推荐
QT setting Icon
彻底搞懂kubernetes调度框架与插件
迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
Dart 代码注释和文档编写规范
Gazebo 控制实例
How to make digital retail undertake the development task of the era of traffic and retention may be the key
“你“想当测试/开发程序员吗?努力发芽的我们......
Unity shader introduction Essentials - basic texture
URDF integrated gazebo
Software test interview question: how to prepare test data? How to prevent data pollution?
MySQL JPA support for JSON type data in database
面试题 01.06. 字符串压缩
Adding custom dynamic arts and Sciences to cesium
Advanced MySQL -- stored procedures and custom functions
Lua快速上手
Storage practices for high-performance computing scenarios, see here
如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
8000字讲透OBSA原理与应用实践
面试题 01.07. 旋转矩阵
普通设备能不能接入TSN时间敏感网络?