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

相关推荐
想回家的一天28 分钟前
雪花算法生成int64,在前端js的精度问题
开发语言·前端·javascript
.又是新的一天.30 分钟前
03_JavaScript
开发语言·javascript·ecmascript
付出不多31 分钟前
python函数与模块
开发语言·python
知识分享小能手42 分钟前
JavaScript学习教程,从入门到精通,XMLHttpRequest 与 Ajax 请求详解(25)
开发语言·javascript·学习·ajax·前端框架·css3·html5
其实你热情似火1 小时前
Java基础第21天-正则表达式
java·开发语言·正则表达式
洛克希德马丁1 小时前
关于Qt对Html/CSS的支持
css·qt·html
卡拉叽里呱啦1 小时前
C#中异步的用法、原则和基本原理
开发语言·c#
wolf犭良1 小时前
37、aiomysql实操习题
开发语言·python·算法
anqi271 小时前
Spark和Hadoop之间的对比和联系
大数据·开发语言·hadoop·spark
向哆哆1 小时前
Java 加密与解密:从算法到应用的全面解析
java·开发语言·算法