Resize QImage

I am coding a small graphics editor and need some help. I draw a QImage as follows:

void Editor::paintEvent(QPaintEvent *event) { QPainter painter(this); // zoom is an int, representing zoomFactor from 1 to 12. painter.drawImage( QRect(0, 0, image.width() * zoom , image.height() * zoom), image); if (zoom >= 3 && showGrid) { painter.setPen(palette().foreground().color()); painter.setPen(Qt::DotLine); // this is how I draw grid for (int i = 0; i <= image.width(); ++i) painter.drawLine(zoom * i, 0, zoom * i, zoom * image.height()); for (int j = 0; j <= image.height(); ++j) painter.drawLine(0, zoom * j, zoom * image.width(), zoom * j); } // (...) } 

It works great with images like this (16 x 16) 1

Faults begin when I open such images (25 X 28)

2

As you can see, pixels are drawn with different widths and heights! What am I doing wrong? Please, help:)

UPD: The problem is solved unexpectedly. I noticed that the editor was QGLWidget, so I tried changing it to QWidget and everything worked fine. Stupid I -_- Btw, maybe there are more convenient ways to zoom in? (For example, cropping pixels that are not needed for coloring)

+4
source share
1 answer

The code for processing very large images was "optimized" some time ago in Qt, and now, unfortunately, it does not work. I didn’t check the code, but my wild guess is that the “speed” or “displacement” texture used for drawing was before it was calculated in a floating point, and now it is calculated using a fixed point.

I don’t remember exactly which version it was introduced with, but it was pretty early after 4.0. We have one of our applications, which should allow us to place the cross with subpixel accuracy and scaling at a point when the scaling factor is high, you can notice that the image is "swinging".

I am the first who does not claim to have an error in the somone else code if I am not 100% sure, but this is one of those cases when I am really 100% sure.

The only way out is to draw the enlarged image manually, redefining the texture display code or (if you only need scaling factors int > 1 ), drawing one pixel at a time with drawRect ... it should be fast enough on the PC.

Please note that the error may be a common error of video drivers, and not an error in Qt ... I saw that the problem with our software is present on different platforms (Windows / Linux / OsX) and is valid only for IIRC when using QWidget ( not when using QGLWidget ).

+1
source

Source: https://habr.com/ru/post/1384387/


All Articles