Pixmap zoom problem



  • I am using Qt5.10 on Mint Linux and when I zoom out on a pixmap I loose part of the image, which is strictly lines. I draw the pixmap using:

    QPainter p(this);
        p.setRenderHint(QPainter::SmoothPixmapTransform);
        p.drawPixmap(this->rect(), *blocks, source);
    

    The addition of the hint made it better but did not fix the problem. The pixmap was created with the lines drawn at a width of 5. Is there some setting I am missing?
    Zoomed in
    zoomed in
    Zoomed out
    zoomed out

    Same area both images.


  • Moderators

    @ofmrew
    try also to enable antialiasing:

    p.setRenderHint(QPainter::Antialiasing);
    


  • @raven-worx Better, but do not fix the problem. Thanks.



  • @ofmrew Tried it on Win10 Qt5.10, same result, but worse.


  • Qt Champions 2016

    @ofmrew

    hi
    Try heavier lines then.
    Normally it can smooth bitmaps ok.

    But you are scaling it down. that might eat lines if scaled to much.



  • @mrjj I tried that, and, yes, it helped; however, the application that I am looking at has user provided scanned images that will be used as a background to be drawn over. There is no control over line thickness, nor even resolution of scans.

    I experimented with the GIMP Image Reader on Mint Linux and I could never get it to lose lines. It would appear that it is in the QPainter::drawPixmap function where a large source rectangle from the pixmap to the smaller target rectangle of widget is the source of the problem. Now to find the solution. I will try experimenting with QImage. Any suggestions are welcome.



  • Found the answer; it required an intermediate step of scaling the pixmap and drawing that pixmap. It work the same with a QImage.

        QPixmap bls = blocks->scaled(this->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
        p.drawPixmap(this->rect(), bls, bls.rect());
    

    Why was this not included as an option in the painter drawPixmap?



  • @ofmrew Update. It also works with a 0 width pen, but is barely visible.


  • Qt Champions 2016

    Ah
    So you "zoomed" it by drawing it into a bigger rect and that did not apply
    smoothing even with QPainter::SmoothPixmapTransform ?
    Or at least not like scaled.



  • @mrjj Let me explain it better. I have drawn a pixmap that is 20,000 by 10,000 and saved that to a PNG file. The widget that I am displaying that image on is 1014 by 657. The first image (see original post) is a 500 by 500 rectangle of the pixmap (aspect ratio adjusted) that is drawn on the widget (Zoomed In). The second image (Zoomed Out) is a rectangle larger that the widget, i.e, more of the pixmap is viewed. As I zoom out more of the lines disappear. raven-worx suggested QPainter::Antialiasing, then I found Qt::SmoothTransformation. Now I can zoom in or zoom out without a problem; however, I have not looked into how response time might be affected by creating a temporary pixmap to display.



  • I used the Image Viewer to open the large file. And it, after changing the zoom out action to allow viewing the full image, drew the complete image and with good response on zooming and panning. I looked at the paintEvent in the QLabel source code and found my solution, namely:

                    d->scaledpixmap = new QPixmap(QPixmap::fromImage(d->cachedimage->scaled(cr.size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    

    I guess great minds think alike. Anyway, ignore my comment about response time as there is none that I can see. I will need to change my widget, MyCanvas, to act like QLabel or subclass QLabel.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.