案例分享:Qt九宫格图片资源浏览器(支持window、linux、兼容各国产系统,支持子文件夹,多选,全选,图片预览,行数与列数设置等)

简介: 案例分享:Qt九宫格图片资源浏览器(支持window、linux、兼容各国产系统,支持子文件夹,多选,全选,图片预览,行数与列数设置等)

需求

  做嵌入式设备,需求九宫格图片资源浏览器:

  1.设置根目录

  2.可拖动;

  3.可设置列数与行数;

  4.点击文件夹可以进入文件夹;

  5.点击图片可以浏览图片;

  6.支持触摸屏上下拽拖浏览;

  7.支持长安出现类似手机更多的操作,用于选择多个图片项做其他操作;

  8.可设置文件排序规则,如大小,更新时间,创建时间等等;


Demo

  

  

  

  

  

  

下载体验地址

  CSDN粉丝免积分下载:https://download.csdn.net/download/qq21497936/15993722

  QQ群:1047134658(点击“文件”搜索“browser”,群内与博文同步更新)


头文件源码 v1.5.1

  

#ifndef MULTIMEDIABROSWERWIDGET_H
#define MULTIMEDIABROSWERWIDGET_H
#include <QWidget>
#include <QDir>
#include <QElapsedTimer>
#include <QTimer>
#include <QLine>
#ifdef designer
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT MultimediaBroswerWidget : public QWidget
#else
class MultimediaBroswerWidget : public QWidget
#endif
{
    Q_OBJECT
public:
    enum SORT_TYPE {
        SORT_TYPE_FILE_NAME_ASC = 0x00,     // 文件名升序(默认)
        SORT_TYPE_FILE_NAME_DES,            // 文件名降序序
        SORT_TYPE_MODIFY_TIME_ASC,          // 文件修改时间升序
        SORT_TYPE_MODIFY_TIME_DES,          // 文件修改时间降序
        SORT_TYPE_SIZE_ASC,                 // 文件大小升序
        SORT_TYPE_SIZE_DES                  // 文件大小降序
    };
public:
    explicit MultimediaBroswerWidget(QWidget *parent = 0);
public:
    QString getRootDirPath()        const;                      // 获取当前设置的根目录路径
    QString getCurrentDirPath()     const;                      // 获取当前所在的目录路径
    QStringList getNameFilters()    const;                      // 获取文件名过滤列表
    QPixmap getBackgroundPixmap()   const;                      // 获取背景图片
    QPixmap getSelectedPixmap()     const;                      // 获取选择状态的按钮图片
    QPixmap getUnSelectedPixmap()   const;                      // 获取非选择状态的按钮图片
    QRect getSelectRect()           const;                      // 获取选择按钮的区域
    int getSelectRightMargin()      const;                      // 获取选择按钮的右边间距
    int getSelectBottomMargin()     const;                      // 获取选择按钮的底部边距
    bool getLongPressed()           const;                      // 获取是否是长按选择后的多选状态
    bool getSelectAll()             const;                      // 获取是否全选
    SORT_TYPE getSortType()         const;                      // 获取分类规则
    QPixmap getFolderPixmap()       const;                      // 获取文件夹图标
    QPixmap getFolderBackgroundPixmap() const;                  // 获取文件夹时的九宫格背景
    bool getCanInOutDir()           const;                      // 获取是否可以进入子文件夹
    bool getInPicture()             const;                      // 是否当前是放大图片的
    int getRows()                   const;                      // 获取显示行数
    int getCols()                   const;                      // 获取显示列数
    QList<QString> getListFile()    const;                      // 获取当前目录下的所有多媒体文件
    QList<QString> getSelectFiles() const;                      // 获取当前选择的所有文件列表
    int getTopMargin()              const;                      // 获取上边框
    int getRightMargin()            const;                      // 获取右边框
    int getBottomMargin()           const;                      // 获取下边框
    int getLeftMargin()             const;                      // 获取左边框
    int getHorizalSpace()           const;                      // 获取水平间隔
    int getVerticalSpace()          const;                      // 获取垂直间隔
public:
    void setRootDirPath(const QString &dirPath);                // 设置预览文件夹路径
    void setCurrentDirPath(const QString &currentDirPath);      // 设置当前文件夹路径
    void setNameFilters(const QStringList &nameFilters);        // 设置文件名过滤
    void setBackgroundPixmap(const QPixmap &backgroundPixmap);  // 设置背景图片
    void setSelectedPixmap(const QPixmap &selectedPixmap);      // 设置选择状态图片
    void setUnSelectedPixmap(const QPixmap &unSelectedPixmap);  // 设置未选择状态的图片
    void setSelectRect(const QRect &selectRect);                // 设置未选择状态图片
    void setSelectRightMargin(int selectRightMargin);           // 设置选择按钮的右边间距
    void setSelectBottomMargin(int selectBottomMargin);         // 设置选择按钮的底部间距
    void setLongPressed(bool longPressed);                      // 设置长按选择后的多选状态
    void selectAll(bool selected = true);                       // 设置进入选择状态,并设置是否全选
    void setSortType(const SORT_TYPE &sortType);                // 设置分类规则
    void setFolderPixmap(const QPixmap &folderPixmap);          // 设置文件夹图标
    void setFolderBackgroundPixmap(const QPixmap &folderBackgroundPixmap);
                                                                // 获取文件夹时的九宫格背景
    void setCanInOutDir(bool canInOutDir);                      // 设置是否可以进入下一级返回上一级文件夹(
                                                                // false的时候不显示子文件夹)
    void setRows(int rows);                                     // 设置显示行数
    void setCols(int value);                                    // 设置显示列数
    void setTopMargin(int topMargin);                           // 设置上边框
    void setRightMargin(int rightMargin);                       // 设置右边框
    void setBottomMargin(int bottomMargin);                     // 设置下边框
    void setLeftMargin(int leftMargin);                         // 设置左边框
    void setMargin(int topMargin, int rightMargin, int bottomMargin, int leftMargin);   // 设置边框
    void setHorizalSpace(int horizalSpace);                     // 设置水平间隔
    void setVerticalSpace(int verticalSpace);                   // 设置垂直间隔
    void setInPicture(bool inPicture);                          // 是否当前是放大图片的
public:
    void reload();                                              // 重新加载缓存
    void keyLeft();
    void keyRight();
    void keyOk();
protected:
    void updateRects();     // 更新位置
    void updateSlider();    // 更新滑动栏
protected slots:
    void slot_timerOut();   // 长按超时
protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
protected:
    void drawBackground(QPainter *painter);
    void drawPixmaps(QPainter *painter);
    void drawSlider(QPainter *painter);
    void drawCurrentPixmap(QPainter *painter);
private:
    QDir _dir;                  // 目录
    QPixmap _backgroundPixmap;  // 背景图片
    QPixmap _unSelectedPixmap;  // 未选中的图片
    QPixmap _selectedPixmap;    // 选中时的图片
    QPixmap _folderPixmap;      // 文件夹图片
    QPixmap _folderBackgroundPixmap;    // 文件夹九宫格项的背景
    QFont _fontFolder;          // 文件夹字体
    QStringList _nameFilters;   // 文件名过滤
    QString _dirPath;           // 目录路径
    QString _rootDirPath;       // 目录根路径
    SORT_TYPE _sortType;        // 排序种类
    QList<QFileInfo> _listFileInfo;   // 可读取的多媒体文件目录
    QList<QPixmap> _listPixmap; // 多媒体文件预览缓存
    QList<bool> _listSelected;  // 是否选中文件
    QRect _selectRect;          // 选择框大小
    int _selectRightMargin;     // 选择框与图片右边的间距
    int _selectBottomMargin;    // 选择框与图片底边的间距
    int _rows;                  // 显示行数
    int _cols;                  // 显示列数
    int _topMargin;             // 上边界
    int _rightMargin;           // 右边界
    int _bottomMargin;          // 下边界
    int _leftMargin;            // 左边界
    int _horizalSpace;          // 水平间隔
    int _verticalSpace;         // 垂直间隔
    int _sliderWidth;           // 滑块宽度
    QRect _sliderHandleRect;    // 滑块矩形
    QRect _sliderRect;          // 滑动条矩形
    int _logicalTop;            // 逻辑顶部
    int _logicalBottom;         // 逻辑底部
    int _logicalHeight;         // 逻辑底部
    int _sliderFreeSpace;       // 滑块余量
    bool _sliderPressed;        // 滑块按下
private:
    float _pixmapWidth;         // 辅助变量,图像宽度
    float _pixmapHeight;        // 辅助变量,图像高度
    QList<QRect> _listRect;     // 多媒体
    int _originY;               // 竖向位置
    bool _pressed;              // 标记是否按下鼠标
    bool _isMoved;              // 标记是否按下鼠标后移动
    QPoint _lastPoint;          // 记录最近一次拖动的鼠标位置
    bool _longPressed;          // 标记是否在长按状态
    bool _longPressedFirst;     // 标记是否刚进入长安状态
    QTimer _timer;              // 用于计时长按操作
    bool _canInOutDir;          // 是否允许进入下一级返回上以及
    bool _inPicture;            // 是否当前是放大图片的
    int _inPictureIndex;
    QPixmap _inPicturePixmap;
};
#endif // MULTIMEDIABROSWERWIDGET_H



相关文章
|
24天前
|
Linux Shell Windows
Linux如何检查文件夹的大小?
在Linux中查看文件夹大小,可使用`du`(如`du -sh *`)、`df`(如`df -h /home`)查看磁盘使用情况,`ncdu`提供交互式浏览,`ls -lh`显示当前目录文件大小,`find`与`du`结合找大文件夹,`tree --du -h`展示目录结构及大小,或用`awk`与`du`组合按大小排序。不同场景下,这些命令各有优势。
148 1
Linux如何检查文件夹的大小?
|
2月前
|
IDE 开发工具 C++
QT案例IDE编写 -- 创建项目
QT案例IDE编写 -- 创建项目
26 0
|
2月前
|
数据挖掘 C++
QT基础入门——项目案例(七)
QT基础入门——项目案例(七)
97 0
QT基础入门——项目案例(七)
|
1月前
|
Linux 编译器 程序员
【Linux 调试秘籍】深入探索 C++:运行时获取堆栈信息和源代码行数的终极指南
【Linux 调试秘籍】深入探索 C++:运行时获取堆栈信息和源代码行数的终极指南
68 0
|
1月前
|
开发框架 算法 Linux
【知识点回顾 】Qt信号槽与Linux信号处理 的处理机制 深入探讨
【知识点回顾 】Qt信号槽与Linux信号处理 的处理机制 深入探讨
35 0
|
1月前
|
存储 缓存 运维
linux 系统中都有哪些文件夹,都是干什么的?
linux 系统中都有哪些文件夹,都是干什么的?
44 0
|
1月前
|
Linux 开发工具 C语言
【研究Qt webengine 模块编译】linux 交叉编译qt5.12的webengine模块成功的条件
【研究Qt webengine 模块编译】linux 交叉编译qt5.12的webengine模块成功的条件
59 1
|
2月前
|
Web App开发
将B站视频设置为浏览器背景,你确定不试试?
将B站视频设置为浏览器背景,你确定不试试?
33 0
|
2月前
|
编解码 IDE 开发工具
QT案例IDE编写 -- 另存为操作及编辑操作
QT案例IDE编写 -- 另存为操作及编辑操作
18 0
|
2月前
|
存储 自然语言处理
QT案例词典 -- 存储内容及遍历
QT案例词典 -- 存储内容及遍历
12 1