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

相关推荐
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna3 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar4 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖5 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
mahuifa5 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
冷眼看人间恩怨5 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
信号处理学渣5 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客5 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
jasmine s5 小时前
Pandas
开发语言·python