当前位置:网站首页>QT - plot other problems
QT - plot other problems
2022-07-04 21:48:00 【Half-up】
1. Redraw events
All the drawing operations mentioned above are in Redraw event handler paintEvent() Done in , It is QWidget Functions defined in class .
One Redraw events are used to redraw all or part of a part .
Redrawing occurs for any of the following reasons :
- repaint() Function or update() Function called ;
- Hidden parts are now redisplayed ;
- Other reasons ;
Most components can simply redraw their entire interface , But some Slow drawing parts need to be optimized and only the required areas should be drawn ( have access to QPaintEvent::region()
To get the area ), This speed optimization does not affect the results .
Qt It will be Speed up rendering by merging multiple redrawing events into one event , When update()
The function is called many times , perhaps The window system sent multiple redrawing events
when , that Qt It will merge these events into one event , And this event has the largest area to redraw .update() function
It will not be redrawn immediately , Wait until the Qt Only after returning to the main event loop , So multiple calls update() function
Generally, it will only cause once paintEvent() function
call .
And call repaint() function
Will immediately call paintEvent() function
To redraw parts , Only if you must redraw immediately ( For example, in animation ) , Only use repaint() function
.
update()
allow Qt Optimize speed and reduce flicker , however repaint() function
Such optimization is not supported , So suggest ― In general, try to use update() function
.
Let me also explain , When the program starts to run, it will automatically send a redraw event and call paintEvent() function . in addition , Not in paintEvent() Call in function update() perhaps repaint() function
.
When When the redrawing event occurs , The area to be updated is usually erased , Then draw on the background of the part .
The background of the part can generally be used setBackgroundRole()
To specify the
And then use setAutoFillBackground( true)
To enable the specified color .
for example , Make the interface display darker colors , You can add the following code to the constructor of the part :
setBackgroundRole(QPalette::Dark); // Black background
setAutoFillBackground(true); // Enable the set background color
2. shear
QPainter
You can cut any drawing operation , It can cut a rectangle 、 Content in an area or a path , This can be used separately setClipRect( ) , setClipRegion() and setClipPath() function
To achieve .
The shear will be in QPainter Logical coordinate system
In the middle of .
The following code implements cutting text in a rectangle :
QPainter painter(this);
painter.setClipRect(10,0,20,10); // Clip
painter.drawText(10,10,tr("img")); // Draw characters ,img
3. Reading and writing images
To read an image , The most common method is 1. Use QImage perhaps QPixmap Constructor for
, Or call 2. QImage::load() and QPixmap::load()
function .
Qt One more QImageReader class
, This kind Provides a format independent interface , You can read images from files or other devices .QImageReader class
You can read the image Provide more control , for example , have access to setScaledSize() function
Read the image in the specified size , You can also use setClipRect()
Read an area of the image .
Because it depends on the underlying support of image format ,QImageReader
These operations can save memory and speed up image reading .
in addition ,Qt It also provides QImageWriter class
Come on Store the image
, it Support specific options for image formatting , For example, gamma level 、 Compression grade and quality, etc . Of course , If you don't need to set these options , So you can use it directly QImage::save() and QPixmap::save() function
.
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QImage image;
// Load a picture
image.load("../mydrawing3/image.png");
// Output some information of the picture
qDebug() << image.size() << image.format() << image.depth();
// Draw pictures on the interface
painter.drawImage(QPoint(10, 10), image);
// Get a mirror image
QImage mirror = image.mirrored();
// Twist the picture
QTransform transform;
transform.shear(0.2, 0);
QImage image2 = mirror.transformed(transform);
painter.drawImage(QPoint(10, 160), image2);
// Save the image to a file
image2.save("../mydrawing3/mirror.png");
}
preservation
verification
4. Play gif Animation
QMovie class
It's using QImageReader
To play the convenient class of animation , Use it to play simple animations without sound , such as gif File format . This class provides a very convenient function to start animation 、 Pause and stop, etc .
The back can speak
5. Rendering SVG file
Scalable vector graphics (Scalable Vector Graphics,SVG)
It's a use XML
To describe Two dimensional graphics and graphics application language .
stay Qt Can be used in QSvgWidget class
Load one SVG file , While using QSvgRenderer class
stay QSvgWidget
In the middle of SVG File rendering .
The use of these two classes is simple , Let's not talk about , You can refer to SVG Generator Example and SVG Viewer Example The sample program .
/
/
/
/
/
/
/
/ If there are others, please add .
边栏推荐
- Redis03 - network configuration and heartbeat mechanism of redis
- Shutter textfield example
- Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
- Lambdaquerywrapper usage
- Flutter TextField示例
- Rotary transformer string judgment
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
- Redis cache
- How to implement Devops with automatic tools
- Case sharing | integrated construction of data operation and maintenance in the financial industry
猜你喜欢
Flutter TextField示例
Word文档中标题前面的黑点如何去掉
SolidWorks工程图添加材料明细表的操作
Operation of adding material schedule in SolidWorks drawing
Jerry added the process of turning off the touch module before turning it off [chapter]
How to use concurrentlinkedqueue as a cache queue
Jerry's ad series MIDI function description [chapter]
Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
Compréhension approfondie du symbole [langue C]
【微信小程序】协同工作与发布
随机推荐
GTEST from ignorance to skillful use (1) GTEST installation
如何使用ConcurrentLinkedQueue做一个缓存队列
bizchart+slider实现分组柱状图
redis布隆过滤器
MP3是如何诞生的?
【活动早知道】LiveVideoStack近期活动一览
如何借助自动化工具落地DevOps
redis RDB AOF
解析互联网时代的创客教育技术
[buuctf.reverse] 151_ [FlareOn6]DnsChess
改善机器视觉系统的方法
[early knowledge of activities] list of recent activities of livevideostack
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
Jerry's ad series MIDI function description [chapter]
Flutter WebView示例
Golang面试整理 三 简历如何书写
maya灯建模
旋变串判断
Go language loop statement (3 in Lesson 10)
new IntersectionObserver 使用笔记