当前位置:网站首页>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 

 Insert picture description here

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

 Insert picture description here

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");
}

 Insert picture description here
preservation
 Insert picture description here
verification
 Insert picture description here

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 .

原网站

版权声明
本文为[Half-up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042048590040.html