当前位置: 首页 > article >正文

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


http://www.kler.cn/a/287609.html

相关文章:

  • 解决 IDEA 修改代码重启不生效的问题
  • Linux最深刻理解页表于物理内存
  • VuePress v2 快速搭建属于自己的个人博客网站
  • OpenTelemetry 赋能DevOps流程的可观测性革命
  • SOP搭建:企业标准化操作程序构建与实施指南
  • 机器学习-36-对ML的思考之机器学习研究的初衷及科学研究的期望
  • Protocol Buffers
  • java打包jar后读取依赖jar包中的文件资源,支持读取jlink打包的模块镜像中读取
  • axure9树形元件节点的添加
  • SQL-多表查询
  • 深入理解Java集合:如何使用for增强循环和泛型类型转换
  • 笔记整理—uboot番外(1)命令体系
  • 大佬借助ChatGPT写论文发刊到手软,四个步骤20个顶级学术提示词指令
  • MyBatis-SQL-语句执行流程
  • UE5 UMG UI编辑器工作流
  • mybatis if标签判断字符串是否相等
  • 面试基本内容
  • 【GD32】RT-Thread实时操作系统移植(GD32F470ZGT6)
  • 中介者模式详解
  • Pytorch实现多层LSTM模型,并增加emdedding、Dropout、权重共享等优化
  • Python 爬虫爬取京东商品信息
  • 会赢的!(牛客)
  • 买电脑如何选择显卡?
  • 10、Flink 动态表之更新和追加查询详解
  • 【React】Redux-toolkit 处理异步操作
  • 网络是怎样连接的