Project combat: Qt Jiugongge image resource browser (supports window, linux, compatible with various countries' production systems, supports subfolders, multiple selection, selection all, picture preview, row and column number settings, etc.)

If the article is an original article, please indicate the original source for reprinting.
The blog address of this article: https://blog.csdn.net/qq21497936/article/details/115035611 will
continue to bring more projects and technology sharing for a long time, please add QQ for inquiries :21497936, WeChat: yangsir198808
Red Fatty (Red Imitation)'s blog post collection: development technology collection (including Qt practical technology, Raspberry Pi, 3D, OpenCV, OpenGL, ffmpeg, OSG, MCU, software and hardware combination, etc.) is being continuously updated … (Click on the portal)

Development column: actual combat of commercial projects


demand

  For embedded devices, Jiugongge image resource browser is required:
  1. Set the root directory;
  2. Can be dragged;
  3. The number of columns and rows can be set;
  4. Click the folder to enter the folder;
  5. Click the image to browse Pictures;
  6. Support drag up and down on the touch screen to browse;
  7. Support more operations similar to mobile phones in Changan, which are used to select multiple picture items for other operations;
  8. File sorting rules can be set, such as size, update time, and creation time and many more;


Demo

  Insert picture description here
  Insert picture description here
  Insert picture description here
  Insert picture description here
  Insert picture description here
  Insert picture description here

Download experience address

  Free points download for CSDN fans: https://download.csdn.net/download/qq21497936/15993722
  QQ group: 1047134658 (click "file" to search for "browser", the group will be updated simultaneously with the blog post)


V1.5.1

  Insert picture description here

#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

If the article is an original article, please indicate the original source for reprinting.
The blog address of this article: https://blog.csdn.net/qq21497936/article/details/115035611

Guess you like

Origin blog.csdn.net/qq21497936/article/details/115035611