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

相关推荐
一点媛艺32 分钟前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风36 分钟前
《Kotlin实战》-附录
android·开发语言·kotlin
奋斗的小花生2 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功2 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
闲晨2 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程2 小时前
一个例子来说明Ada语言的实时性支持
开发语言·ada
Chrikk3 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*3 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue3 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man3 小时前
【go从零单排】go语言中的指针
开发语言·后端·golang