Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qfilesystemmodel.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QFILESYSTEMMODEL_H
5#define QFILESYSTEMMODEL_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtCore/qabstractitemmodel.h>
9#include <QtCore/qpair.h>
10#include <QtCore/qdir.h>
11#include <QtGui/qicon.h>
12#include <QtCore/qdiriterator.h>
13
14QT_REQUIRE_CONFIG(filesystemmodel);
15
17
18class ExtendedInformation;
21
22class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel
23{
25 Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
26 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
27 Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
28 Q_PROPERTY(Options options READ options WRITE setOptions)
29
31 void rootPathChanged(const QString &newPath);
32 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
34
35public:
36 enum Roles {
37 FileIconRole = Qt::DecorationRole,
38 FilePathRole = Qt::UserRole + 1,
39 FileNameRole = Qt::UserRole + 2,
40 FilePermissions = Qt::UserRole + 3
41 };
42
43 enum Option
44 {
45 DontWatchForChanges = 0x00000001,
46 DontResolveSymlinks = 0x00000002,
47 DontUseCustomDirectoryIcons = 0x00000004
48 };
51
52 explicit QFileSystemModel(QObject *parent = nullptr);
54
55 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
56 QModelIndex index(const QString &path, int column = 0) const;
57 QModelIndex parent(const QModelIndex &child) const override;
58 using QObject::parent;
59 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
60 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
61 bool canFetchMore(const QModelIndex &parent) const override;
62 void fetchMore(const QModelIndex &parent) override;
63
64 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
65 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
66
67 QVariant myComputer(int role = Qt::DisplayRole) const;
68 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
69 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
70
71 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
72
73 Qt::ItemFlags flags(const QModelIndex &index) const override;
74
75 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
76
77 QStringList mimeTypes() const override;
78 QMimeData *mimeData(const QModelIndexList &indexes) const override;
79 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
80 int row, int column, const QModelIndex &parent) override;
81 Qt::DropActions supportedDropActions() const override;
82 QHash<int, QByteArray> roleNames() const override;
83
84 // QFileSystemModel specific API
85 QModelIndex setRootPath(const QString &path);
86 QString rootPath() const;
87 QDir rootDirectory() const;
88
89 void setIconProvider(QAbstractFileIconProvider *provider);
90 QAbstractFileIconProvider *iconProvider() const;
91
92 void setFilter(QDir::Filters filters);
93 QDir::Filters filter() const;
94
95 void setResolveSymlinks(bool enable);
96 bool resolveSymlinks() const;
97
98 void setReadOnly(bool enable);
99 bool isReadOnly() const;
100
101 void setNameFilterDisables(bool enable);
102 bool nameFilterDisables() const;
103
105 QStringList nameFilters() const;
106
107 void setOption(Option option, bool on = true);
108 bool testOption(Option option) const;
109 void setOptions(Options options);
110 Options options() const;
111
112 QString filePath(const QModelIndex &index) const;
113 bool isDir(const QModelIndex &index) const;
114 qint64 size(const QModelIndex &index) const;
115 QString type(const QModelIndex &index) const;
116
117 // ### Qt7 merge the two overloads, with tz QTimeZone::LocalTime
118 QDateTime lastModified(const QModelIndex &index) const;
119 QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const;
120
121 QModelIndex mkdir(const QModelIndex &parent, const QString &name);
122 bool rmdir(const QModelIndex &index);
123 inline QString fileName(const QModelIndex &index) const;
124 inline QIcon fileIcon(const QModelIndex &index) const;
125 QFile::Permissions permissions(const QModelIndex &index) const;
126 QFileInfo fileInfo(const QModelIndex &index) const;
127 bool remove(const QModelIndex &index);
128
129protected:
131 void timerEvent(QTimerEvent *event) override;
132 bool event(QEvent *event) override;
133
134private:
135 Q_DECLARE_PRIVATE(QFileSystemModel)
136 Q_DISABLE_COPY(QFileSystemModel)
137
138 Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list))
139 Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort())
140 Q_PRIVATE_SLOT(d_func(),
141 void _q_fileSystemChanged(const QString &path,
142 const QList<QPair<QString, QFileInfo>> &))
143 Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName))
144
145 friend class QFileDialogPrivate;
146};
147
148inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
149{ return aindex.data(Qt::DisplayRole).toString(); }
151{ return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
152
153Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
154
156
157#endif // QFILESYSTEMMODEL_H
virtual Qt::DropActions supportedDropActions() const
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
virtual Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
virtual Q_INVOKABLE bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Returns {true} if parent has any children; otherwise returns {false}.
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
virtual Q_INVOKABLE void fetchMore(const QModelIndex &parent)
Fetches any available data for the items with the parent specified by the parent index.
virtual Q_INVOKABLE bool canFetchMore(const QModelIndex &parent) const
Returns {true} if there is more data available for parent; otherwise returns {false}.
virtual Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
virtual Q_INVOKABLE QModelIndex sibling(int row, int column, const QModelIndex &idx) const
Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no...
virtual QHash< int, QByteArray > roleNames() const
virtual Q_INVOKABLE int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
virtual QStringList mimeTypes() const
Returns the list of allowed MIME types.
\inmodule QtCore\reentrant
Definition qdatetime.h:257
\inmodule QtCore
Definition qdir.h:19
\inmodule QtCore
Definition qcoreevent.h:45
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
The QFileSystemModel class provides a data model for the local filesystem.
void fileRenamed(const QString &path, const QString &oldName, const QString &newName)
This signal is emitted whenever a file with the oldName is successfully renamed to newName.
QIcon fileIcon(const QModelIndex &index) const
Returns the icon for the item stored in the model under the given index.
void rootPathChanged(const QString &newPath)
This signal is emitted whenever the root path has been changed to a newPath.
~QFileSystemModel()
Destroys this file system model.
Roles
\value FileIconRole \value FilePathRole \value FileNameRole \value FilePermissions
void directoryLoaded(const QString &path)
\inmodule QtCore
Definition qhash.h:818
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1433
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qtimezone.h:25
\inmodule QtCore
Definition qcoreevent.h:359
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:97
@ UserRole
@ DecorationRole
@ EditRole
@ DisplayRole
SortOrder
Definition qnamespace.h:120
@ AscendingOrder
Definition qnamespace.h:121
DropAction
std::pair< T1, T2 > QPair
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLenum type
GLbitfield flags
GLboolean enable
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
GLbyte GLbyte tz
struct _cl_event * event
GLsizei const GLchar *const * path
GLenum GLenum GLsizei void * row
GLuint GLenum option
GLfixed GLfixed GLint GLint order
#define QT_REQUIRE_CONFIG(feature)
#define Q_ENUM(x)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
long long qint64
Definition qtypes.h:55
QList< int > list
[14]
settings remove("monkey")
QMimeData * mimeData
dialog setNameFilters(filters)
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
QLayoutItem * child
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent