QT实现的具有放大功能的小窗口

当小窗口在图片上方时,显示放大的局部图片,效果如下图所示:

源码下载地址:QT实现的具有放大图像功能的小窗口-互联网文档类资源-CSDN下载icon-default.png?t=LA92https://download.csdn.net/download/hulinhulin/44741073

注意:图片是按原图比例显示的,图片显示时已经位移或者缩放了,需要重新计算子图像位置。

1 下方图片的显示

在paintEvent中显示:

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

painter.drawPixmap(0,0,pixmap);

2 获取小窗口下面的图像

为了加快速度,先从原图获取部分图像,然后再将获取到的子图像放大。

扫描二维码关注公众号,回复: 15052902 查看本文章

 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 小窗口中显示放大的图像

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

猜你喜欢

转载自blog.csdn.net/hulinhulin/article/details/121417358
今日推荐