File indexing completed on 2025-09-17 09:09:59
0001
0002
0003
0004 #ifndef QFILESYSTEMMODEL_H
0005 #define QFILESYSTEMMODEL_H
0006
0007 #include <QtGui/qtguiglobal.h>
0008 #include <QtCore/qabstractitemmodel.h>
0009 #include <QtCore/qdir.h>
0010 #include <QtGui/qicon.h>
0011
0012 QT_REQUIRE_CONFIG(filesystemmodel);
0013
0014 QT_BEGIN_NAMESPACE
0015
0016 class ExtendedInformation;
0017 class QFileSystemModelPrivate;
0018 class QAbstractFileIconProvider;
0019
0020 class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel
0021 {
0022 Q_OBJECT
0023 Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
0024 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
0025 Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
0026 Q_PROPERTY(Options options READ options WRITE setOptions)
0027
0028 Q_SIGNALS:
0029 void rootPathChanged(const QString &newPath);
0030 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
0031 void directoryLoaded(const QString &path);
0032
0033 public:
0034 enum Roles {
0035 FileIconRole = Qt::DecorationRole,
0036
0037 FileInfoRole = Qt::UserRole - 4,
0038 QT7_ONLY(
0039 FilePathRole = Qt::UserRole - 3,
0040 FileNameRole = Qt::UserRole - 2,
0041 FilePermissions = Qt::UserRole - 1,
0042 )
0043
0044 QT6_ONLY(
0045 FilePathRole = Qt::UserRole + 1,
0046 FileNameRole = Qt::UserRole + 2,
0047 FilePermissions = Qt::UserRole + 3,
0048 )
0049 };
0050
0051 enum Option
0052 {
0053 DontWatchForChanges = 0x00000001,
0054 DontResolveSymlinks = 0x00000002,
0055 DontUseCustomDirectoryIcons = 0x00000004
0056 };
0057 Q_ENUM(Option)
0058 Q_DECLARE_FLAGS(Options, Option)
0059
0060 explicit QFileSystemModel(QObject *parent = nullptr);
0061 ~QFileSystemModel();
0062
0063 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0064 QModelIndex index(const QString &path, int column = 0) const;
0065 QModelIndex parent(const QModelIndex &child) const override;
0066 using QObject::parent;
0067 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
0068 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
0069 bool canFetchMore(const QModelIndex &parent) const override;
0070 void fetchMore(const QModelIndex &parent) override;
0071
0072 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0073 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0074
0075 QVariant myComputer(int role = Qt::DisplayRole) const;
0076 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0077 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0078
0079 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0080
0081 Qt::ItemFlags flags(const QModelIndex &index) const override;
0082
0083 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
0084
0085 QStringList mimeTypes() const override;
0086 QMimeData *mimeData(const QModelIndexList &indexes) const override;
0087 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
0088 int row, int column, const QModelIndex &parent) override;
0089 Qt::DropActions supportedDropActions() const override;
0090 QHash<int, QByteArray> roleNames() const override;
0091
0092
0093 QModelIndex setRootPath(const QString &path);
0094 QString rootPath() const;
0095 QDir rootDirectory() const;
0096
0097 void setIconProvider(QAbstractFileIconProvider *provider);
0098 QAbstractFileIconProvider *iconProvider() const;
0099
0100 void setFilter(QDir::Filters filters);
0101 QDir::Filters filter() const;
0102
0103 void setResolveSymlinks(bool enable);
0104 bool resolveSymlinks() const;
0105
0106 void setReadOnly(bool enable);
0107 bool isReadOnly() const;
0108
0109 void setNameFilterDisables(bool enable);
0110 bool nameFilterDisables() const;
0111
0112 void setNameFilters(const QStringList &filters);
0113 QStringList nameFilters() const;
0114
0115 void setOption(Option option, bool on = true);
0116 bool testOption(Option option) const;
0117 void setOptions(Options options);
0118 Options options() const;
0119
0120 QString filePath(const QModelIndex &index) const;
0121 bool isDir(const QModelIndex &index) const;
0122 qint64 size(const QModelIndex &index) const;
0123 QString type(const QModelIndex &index) const;
0124
0125 QDateTime lastModified(const QModelIndex &index) const;
0126 QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const;
0127
0128 QModelIndex mkdir(const QModelIndex &parent, const QString &name);
0129 bool rmdir(const QModelIndex &index);
0130 inline QString fileName(const QModelIndex &index) const;
0131 inline QIcon fileIcon(const QModelIndex &index) const;
0132 QFile::Permissions permissions(const QModelIndex &index) const;
0133 QFileInfo fileInfo(const QModelIndex &index) const;
0134 bool remove(const QModelIndex &index);
0135
0136 protected:
0137 QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr);
0138 void timerEvent(QTimerEvent *event) override;
0139 bool event(QEvent *event) override;
0140
0141 private:
0142 Q_DECLARE_PRIVATE(QFileSystemModel)
0143 Q_DISABLE_COPY(QFileSystemModel)
0144
0145 friend class QFileDialogPrivate;
0146 };
0147
0148 inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
0149 {
0150 return aindex.data(FileNameRole).toString();
0151 }
0152 inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
0153 { return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
0154
0155 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
0156
0157 QT_END_NAMESPACE
0158
0159 #endif