Qt图片滚动预览

本文介绍了如何使用Qt库创建一个图片滚动预览的功能,详细讲解了关键代码和实现步骤,包括图片加载、布局管理以及平滑滚动效果的实现。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#pragma execution_character_set("utf-8")

#ifndef PICTUREFLOW_H
#define PICTUREFLOW_H

#include <QWidget>

class PictureFlowPrivate;

class PictureFlow : public QWidget
{
Q_OBJECT

  Q_PROPERTY(int slideCount READ slideCount WRITE setSlideCount)
  Q_PROPERTY(int currentSlide READ currentSlide WRITE setCurrentSlide)
  Q_PROPERTY(QSize slideSize READ slideSize WRITE setSlideSize)
  Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor)

public:
  /*!
    Creates a new PictureFlow widget.
  */
  PictureFlow(QWidget* parent = 0);

  /*!
    Destroys the widget.
  */
  ~PictureFlow();

  /*!
    Returns the total number of slides.
  */
  int slideCount() const;

  /*!
    Sets the total number of slides.
  */
  void setSlideCount(int count);

  /*!
    Returns the dimension of each slide (in pixels).
  */
  QSize slideSize() const;

  /*!
    Sets the dimension of each slide (in pixels).
  */
  void setSlideSize(QSize size);

  /*!
    Sets the zoom factor (in percent).
  */
  void setZoomFactor(int zoom);

  /*!
    Returns the zoom factor (in percent).
  */
  int zoomFactor() const;

  /*!
    Clears any caches held to free up memory
  */
  void clearCaches();

  /*!
    Returns QImage of specified slide.
    This function will be called only whenever necessary, e.g. the 100th slide
    will not be retrived when only the first few slides are visible.
  */
  virtual QImage slide(int index) const;

  /*!
    Sets an image for specified slide. If the slide already exists,
    it will be replaced.
  */
  virtual void setSlide(int index, const QImage& image);

  virtual void setSlideCaption(int index, QString caption);

  /*!
    Sets a pixmap for specified slide. If the slide already exists,
    it will be replaced.
  */
  virtual void setSlide(int index, const QPixmap& pixmap);

  /*!
    Returns the index of slide currently shown in the middle of the viewport.
  */
  int currentSlide() const;

public slots:

  /*!
    Sets slide to be shown in the middle of the viewport. No animation
    effect will be produced, unlike using showSlide.
  */
  void setCurrentSlide(int index);

  /*!
    Clears images of all slides.
  */
  void clear();

  /*!
    Rerender the widget. Normally this function will be automatically invoked
    whenever necessary, e.g. during the transition animation.
  */
  void render();

  /*!
    Shows previous slide using animation effect.
  */
  void showPrevious();

  /*!
    Shows next slide using animation effect.
  */
  void showNext();

  /*!
    Go to specified slide using animation effect.
  */
  void showSlide(int index);

signals:
  void itemActivated(int index);
  void inputReceived();

protected:
  void paintEvent(QPaintEvent *event) override;
  void keyPressEvent(QKeyEvent *event) override;
  void mouseMoveEvent(QMouseEvent *event) override;
  void mousePressEvent(QMouseEvent *event) override;
  void mouseReleaseEvent(QMouseEvent *event) override;
  void resizeEvent(QResizeEvent *event) override;
  void timerEvent(QTimerEvent *event) override;

private:
  PictureFlowPrivate* d;
};

#endif // PICTUREFLOW_H


#include "pictureflow.h"

#include <QBasicTimer>
#include <QCache>
#include <QImage>
#include <QKeyEvent>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QVector>
#include <QWidget>
#include <QTime>

#include <QDebug>

static const int captionFontSize =
#ifdef Q_WS_S60
    8;
#else
    14;
#endif


// uncomment this to enable bilinear filtering for texture mapping
// gives much better rendering, at the cost of memory space
// #define PICTUREFLOW_BILINEAR_FILTER

// for fixed-point arithmetic, we need minimum 32-bit long
// long long (64-bit) might be useful for multiplication and division
typedef long PFreal;

typedef unsigned short QRgb565;

#define RGB565_RED_MASK 0xF800
#define RGB565_GREEN_MASK 0x07E0
#define RGB565_BLUE_MASK 0x001F

#define RGB565_RED(col) ((col&RGB565_RED_MASK)>>11)
#define RGB565_GREEN(col) ((col&RGB565_GREEN_MASK)>>5)
#define RGB565_BLUE(col) (col&RGB565_BLUE_MASK)

#define PFREAL_SHIFT 10
#define PFREAL_FACTOR (1 << PFREAL_SHIFT)
#define PFREAL_ONE (1 << PFREAL_SHIFT)
#define PFREAL_HALF (PFREAL_ONE >> 1)

inline PFreal fmul(PFreal a, PFreal b)
{
  return ((long long)(a))*((long long)(b)) >> PFREAL_SHIFT;
}

inline PFreal fdiv(PFreal num, PFreal den)
{
  long long p = (long long)(num) << (PFREAL_SHIFT*2);
  long long q = p / (long long)den;
  long long r = q >> PFREAL_SHIFT;

  return r;
}

inline float fixedToFloat(PFreal val)
{
  return ((float)val) / (float)PFREAL_ONE;
}

inline PFreal floatToFixed(float val)
{
  return (PFreal)(val*PFREAL_ONE);
}

#define IANGLE_MAX 1024
#define IANGLE_MASK 1023

// warning: regenerate the table if IANGLE_MAX and PFREAL_SHIFT are changed!
static const PFreal sinTable[IANGLE_MAX] = {
     3,      9,     15,     21,     28,     34,     40,     47,
    53,     59,     65,     72,     78,     84,     90,     97,
   103,    109,    115,    122,    128,    134,    140,    147,
   153,    159,    165,    171,    178,    184,    190,    196,
   202,    209,    215,    221,    227,    233,    239,    245,
   251,    257,    264,    270,    276,    282,    288,    294,
   300,    306,    312,    318,    324,    330,    336,    342,
   347,    353,    359,    365,    371,    377,    383,    388,
   394,    400,    406,    412,    417,    423,    429,    434,
   440,    446,    451,    457,    463,    468,    474,    479,
   485,    491,    496,    501,    507,    512,    518,    523,
   529,    534,    539,    545,    550,    555,    561,    566,
   571,    576,    581,    587,    592,    597,    602,    607,
   612,    617,    622,    627,    632,    637,    642,    647,
   652,    656,    661,    666,    671,    675,    680,    685,
   690,    694,    699,    703,    708,    712,    717,    721,
   726,    730,    735,    739,    743,    748,    752,    756,
   760,    765,    769,    773,    777,    781,    785,    789,
   793,    797,    801,    805,    809,    813,    816,    820,
   824,    828,    831,    835,    839,    842,    846,    849,
   853,    856,    860,    863,    866,    870,    873,    876,
   879,    883,    886,    889,    892,    895,    898,    901,
   904,    907,    910,    913,    916,    918,    921,    924,
   927,    929,    932,    934,    937,    939,    942,    944,
   947,    949,    951,    954,    956,    958,    960,    963,
   965,    967,    969,    971,    973,    975,    977,    978,
   980,    982,    984,    986,    987,    989,    990,    992,
   994,    995,    997,    998,    999,   1001,   1002,   1003,
  1004,   1006,   1007,   1008,   1009,   1010,   1011,   1012,
  1013,   1014,   1015,   1015,   1016,   1017,   1018,   1018,
  1019,   1019,   1020,   1020,   1021,   1021,   1022,   1022,
  1022,   1023,   1023,   1023,   1023,   1023,   1023,   1023,
  1023,   1023,   1023,   1023,   1023,   1023,   1023,   1022,
  1022,   1022,   1021,   1021,   1020,   1020,   1019,   1019,
  1018,   1018,   1017,   1016,   1015,   1015,   1014,   1013,
  1012,   1011,   1010,   1009,   1008,   1007,   1006,   1004,
  1003,   1002,   1001,    999,    998,    997,    995,    994,
   992,    990,    989,    987,    986,    984,    982,    980,
   978,    977,    975,    973,    971,    969,    967,    965,
   963,    960,    958,    956,    954,    951,    949,    947,
   944,    942,    939,    937,    934,    932,    929,    927,
   924,    921,    918,    916,    913,    910,    907,    904,
   901,    898,    895,    892,    889,    886,    883,    879,
   876,    873,    870,    866,    863,    860,    856,    853,
   849,    846,    842,    839,    835,    831,    828,    824,
   820,    816,    813,    809,    805,    801,    797,    793,
   789,    785,    781,    777,    773,    769,    765,    760,
   756,    752,    748,    743,    739,    735,    730,    726,
   721,    717,    712,    708,    703,    699,    694,    690,
   685,    680,    675,    671,    666,    661,    656,    652,
   647,    642,    637,    632,    627,    622,    617,    612,
   607,    602,    597,    592,    587,    581,    576,    571,
   566,  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值