Small window with magnification function implemented by QT

When the small window is above the picture, the enlarged partial picture is displayed, and the effect is shown in the figure below:

 

Source code download address: A small window with the function of enlarging images implemented by QT-Internet document resources-CSDN download icon-default.png?t=LA92https://download.csdn.net/download/hulinhulin/44741073

Note: The picture is displayed in proportion to the original picture. When the picture is displayed, it has been shifted or scaled, and the position of the sub-image needs to be recalculated.

1 The display of the picture below

Shown in paintEvent:

QPixmap pixmap("c:/patch-src.png");

painter.drawPixmap(0,0,pixmap);

2 Get the image below the small window

In order to speed up, first obtain part of the image from the original image, and then enlarge the obtained sub-image.

 QImage _subImage;

                getRectSubPix(_srcImage,_curInnerRect.center(),_curInnerRect.width(),_curInnerRect.height(),_subImage);

 QImage _scaledImage = _subImage.scaled(_subImage.width() * _ratio,_subImage.height() * _ratio,Qt::KeepAspectRatio,Qt::SmoothTransformation);

                QPoint _scaledCenter = QPoint(_scaledImage.width() /2 ,_scaledImage.height() / 2);

                getRectSubPix(_scaledImage,_scaledCenter,_curInnerRect.width(),_curInnerRect.height(),scaledSubImage);

3 The enlarged image is displayed in a small window

        QImage _grayImage(this->scaledSubImage.size(),QImage::Format_Alpha8);

        _grayImage.fill(250);

        this->scaledSubImage.setAlphaChannel(_grayImage);

        QPixmap pixmap = QPixmap::fromImage(this->scaledSubImage);

        QRect _drawRect;

        if (this->outerWindow != nullptr){

            _drawRect.setTopLeft(this->rect().topLeft());

            _drawRect.setBottomRight(this->rect().bottomRight());

        }

        if (this->innerWindow != nullptr){

            _drawRect.setTopLeft(this->mapFromParent(this->innerWindow->geometry().topLeft()));

            _drawRect.setBottomRight(this->mapFromParent(this->innerWindow->geometry().bottomRight()));

        }

        _drawRect.adjust(sr,sr,-sr,-sr);

        painter.drawPixmap(_drawRect,pixmap);

Guess you like

Origin blog.csdn.net/hulinhulin/article/details/121417358