QT QGraphicsView实现预览图片显示缩略图功能

QT QGraphicsView实现预览图片显示缩略图功能QT creator Qt5.15.2

头文件:

#ifndef TGRAPHICSVIEW_H
#define TGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QMainWindow>
#include <QObject>
#include <QWidget>

class TGraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    TGraphicsView(QWidget *parent = 0);
private slots:
    void scrollBarValueChanged();

private:
    void resizeEvent(QResizeEvent *event) override;
    void updateThumbRoi();

    struct PosInfo
    {
        int min;
        int max;
        int value;
        int page;
    };

private:
    class Thumb;
    Thumb *thumb;
};
class TGraphicsView::Thumb : public QWidget
{
    Q_OBJECT

public:
    using PosInfo = TGraphicsView::PosInfo;
    Thumb(QWidget* parent = 0);
    void updateImage();
    void updateRoi(const PosInfo& xinfo, const PosInfo& yinfo);

private:
    void paintEvent(QPaintEvent *event) override;

private:
    QPixmap background;
    QRect roi;
};

#endif // TGRAPHICSVIEW_H

cpp文件:

TGraphicsView::TGraphicsView(QWidget *parent) :
    QGraphicsView(parent)
{
    thumb = new Thumb(this);
    QScrollBar* hsb = horizontalScrollBar();
    connect(hsb, &QScrollBar::valueChanged, this, &TGraphicsView::scrollBarValueChanged);
    QScrollBar* vsb = verticalScrollBar();
    connect(vsb, &QScrollBar::valueChanged, this, &TGraphicsView::scrollBarValueChanged);



}


void TGraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
    thumb->updateImage();
    updateThumbRoi();
}

void TGraphicsView::scrollBarValueChanged()
{
    updateThumbRoi();
}

void TGraphicsView::updateThumbRoi()
{
    QScrollBar* hsb = horizontalScrollBar();
    PosInfo xinfo;
    xinfo.min = hsb->minimum();
    xinfo.max = hsb->maximum();
    xinfo.value = hsb->value();
    xinfo.page = hsb->pageStep();
    QScrollBar* vsb = verticalScrollBar();
    PosInfo yinfo;
    yinfo.min = vsb->minimum();
    yinfo.max = vsb->maximum();
    yinfo.value = vsb->value();
    yinfo.page = vsb->pageStep();
    thumb->updateRoi(xinfo, yinfo);
}

/

TGraphicsView::Thumb::Thumb(QWidget* parent) :
    QWidget(parent)
{
    setFixedSize(150, 150);
}

void TGraphicsView::Thumb::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setOpacity(0.8);
    painter.fillRect(QRect(0, 0, width(), height()), QColor(255, 192, 32));
    int xoff = (width() - background.width()) / 2;
    int yoff = (height() - background.height()) / 2;
    painter.drawPixmap(xoff, yoff, background);

    /* 绘制ROI */
    //painter.setPen(QColor(32, 32, 32));
    painter.setPen(QColor(255, 0, 0));
    painter.setBrush(Qt::NoBrush);
    painter.drawRect(roi.adjusted(xoff, yoff, xoff, yoff));
}

void TGraphicsView::Thumb::updateImage()
{
    QGraphicsView* view = dynamic_cast<QGraphicsView*>(parent());
    QRectF rect = view->sceneRect();
    qreal ratio = qMin(width() / rect.width(), height() / rect.height());
    background = QPixmap(rect.width() * ratio, rect.height() * ratio);
    QPainter painter(&background);
    QGraphicsScene* sc = view->scene();
    sc->render(&painter);
    update();
}

void TGraphicsView::Thumb::updateRoi(const PosInfo& xinfo, const PosInfo& yinfo)
{
    int w = background.width();
    int xwhole = ((xinfo.max - xinfo.min) + xinfo.page);
    int roiLeft = w * xinfo.value / xwhole;
    int roiWidth = w * xinfo.page / xwhole;
    int h = background.height();
    int ywhole = ((yinfo.max - yinfo.min) + yinfo.page);
    int roiTop = h * yinfo.value / ywhole;
    int roiHeight = h * yinfo.page / ywhole;
    roi = QRect(roiLeft, roiTop, roiWidth, roiHeight);
    update();
}

工程代码:

https://download.csdn.net/download/txwtech/89700311

相关推荐
重生之我在20年代敲代码1 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文2 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6256 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab