File indexing completed on 2026-08-05 09:27:20
0001
0002
0003
0004
0005 #ifndef QDIR_H
0006 #define QDIR_H
0007
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qstring.h>
0010 #include <QtCore/qdirlisting.h>
0011 #include <QtCore/qfile.h>
0012 #include <QtCore/qfileinfo.h>
0013 #include <QtCore/qstringlist.h>
0014 #include <QtCore/qshareddata.h>
0015
0016 QT_BEGIN_NAMESPACE
0017
0018 class QDirIterator;
0019 class QDirPrivate;
0020
0021 class Q_CORE_EXPORT QDir
0022 {
0023 public:
0024 enum Filter { Dirs = 0x001,
0025 Files = 0x002,
0026 Drives = 0x004,
0027 NoSymLinks = 0x008,
0028 AllEntries = Dirs | Files | Drives,
0029 TypeMask = 0x00f,
0030
0031 Readable = 0x010,
0032 Writable = 0x020,
0033 Executable = 0x040,
0034 PermissionMask = 0x070,
0035 #if QT_DEPRECATED_SINCE(6, 11)
0036 Modified QT_DEPRECATED_VERSION_X_6_11("This flag is not handled in QDir; you can simply remove it from your code.") = 0x080,
0037 #endif
0038 Hidden = 0x100,
0039 System = 0x200,
0040
0041 AccessMask = 0x3F0,
0042
0043 AllDirs = 0x400,
0044 CaseSensitive = 0x800,
0045 NoDot = 0x2000,
0046 NoDotDot = 0x4000,
0047 NoDotAndDotDot = NoDot | NoDotDot,
0048
0049 NoFilter = -1
0050 };
0051 Q_DECLARE_FLAGS(Filters, Filter)
0052
0053 enum SortFlag { Name = 0x00,
0054 Time = 0x01,
0055 Size = 0x02,
0056 Unsorted = 0x03,
0057 SortByMask = 0x03,
0058
0059 DirsFirst = 0x04,
0060 Reversed = 0x08,
0061 IgnoreCase = 0x10,
0062 DirsLast = 0x20,
0063 LocaleAware = 0x40,
0064 Type = 0x80,
0065 NoSort = -1
0066 };
0067 Q_DECLARE_FLAGS(SortFlags, SortFlag)
0068
0069 QDir(const QDir &);
0070 QDir(const QString &path = QString());
0071 QDir(const QString &path, const QString &nameFilter,
0072 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
0073 #ifdef Q_QDOC
0074 QDir(const std::filesystem::path &path);
0075 QDir(const std::filesystem::path &path, const QString &nameFilter,
0076 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
0077 #elif QT_CONFIG(cxx17_filesystem)
0078 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
0079 QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path))
0080 {
0081 }
0082 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
0083 QDir(const T &path, const QString &nameFilter,
0084 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries)
0085 : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter)
0086 {
0087 }
0088 #endif
0089 ~QDir();
0090
0091 QDir &operator=(const QDir &);
0092 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDir)
0093
0094 void swap(QDir &other) noexcept
0095 { d_ptr.swap(other.d_ptr); }
0096
0097 void setPath(const QString &path);
0098 #ifdef Q_QDOC
0099 void setPath(const std::filesystem::path &path);
0100 #elif QT_CONFIG(cxx17_filesystem)
0101 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
0102 void setPath(const T &path)
0103 {
0104 setPath(QtPrivate::fromFilesystemPath(path));
0105 }
0106 #endif
0107 QString path() const;
0108 QString absolutePath() const;
0109 QString canonicalPath() const;
0110 #if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
0111 std::filesystem::path filesystemPath() const
0112 { return QtPrivate::toFilesystemPath(path()); }
0113 std::filesystem::path filesystemAbsolutePath() const
0114 { return QtPrivate::toFilesystemPath(absolutePath()); }
0115 std::filesystem::path filesystemCanonicalPath() const
0116 { return QtPrivate::toFilesystemPath(canonicalPath()); }
0117 #endif
0118
0119 #ifndef QT_BOOTSTRAPPED
0120 static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
0121 static void addSearchPath(const QString &prefix, const QString &path);
0122 #ifdef Q_QDOC
0123 static void addSearchPath(const QString &prefix, const std::filesystem::path &path);
0124 #elif QT_CONFIG(cxx17_filesystem)
0125 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
0126 static void addSearchPath(const QString &prefix, const T &path)
0127 {
0128 addSearchPath(prefix, QtPrivate::fromFilesystemPath(path));
0129 }
0130 #endif
0131 static QStringList searchPaths(const QString &prefix);
0132 #endif
0133
0134 QString dirName() const;
0135 QString filePath(const QString &fileName) const;
0136 QString absoluteFilePath(const QString &fileName) const;
0137 QString relativeFilePath(const QString &fileName) const;
0138
0139 static QString toNativeSeparators(const QString &pathName);
0140 static QString fromNativeSeparators(const QString &pathName);
0141
0142 bool cd(const QString &dirName);
0143 bool cdUp();
0144
0145 QStringList nameFilters() const;
0146 void setNameFilters(const QStringList &nameFilters);
0147
0148 Filters filter() const;
0149 void setFilter(Filters filter);
0150 SortFlags sorting() const;
0151 void setSorting(SortFlags sort);
0152
0153 #if QT_CORE_REMOVED_SINCE(6, 5)
0154 uint count() const;
0155 #endif
0156 qsizetype count(QT6_DECL_NEW_OVERLOAD) const;
0157 bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const;
0158
0159 #if QT_CORE_REMOVED_SINCE(6, 5) && QT_POINTER_SIZE != 4
0160 QString operator[](int) const;
0161 #endif
0162 QString operator[](qsizetype) const;
0163
0164 static QStringList nameFiltersFromString(const QString &nameFilter);
0165
0166 QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
0167 QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
0168 SortFlags sort = NoSort) const;
0169
0170 QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
0171 QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
0172 SortFlags sort = NoSort) const;
0173
0174 #if QT_CORE_REMOVED_SINCE(6, 10)
0175 bool mkdir(const QString &dirName) const;
0176 bool mkdir(const QString &dirName, QFile::Permissions permissions) const;
0177 #endif
0178 bool mkdir(const QString &dirName, std::optional<QFile::Permissions> p = std::nullopt) const;
0179 bool rmdir(const QString &dirName) const;
0180 #if QT_CORE_REMOVED_SINCE(6, 10)
0181 bool mkpath(const QString &dirPath) const;
0182 #endif
0183 bool mkpath(const QString &dirPath, std::optional<QFile::Permissions> p = std::nullopt) const;
0184 bool rmpath(const QString &dirPath) const;
0185
0186 bool removeRecursively();
0187
0188 bool isReadable() const;
0189 bool exists() const;
0190 bool isRoot() const;
0191
0192 static bool isRelativePath(const QString &path);
0193 inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
0194 bool isRelative() const;
0195 inline bool isAbsolute() const { return !isRelative(); }
0196 bool makeAbsolute();
0197
0198 #if QT_CORE_REMOVED_SINCE(6, 8)
0199 bool operator==(const QDir &dir) const;
0200 inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
0201 #endif
0202
0203 bool remove(const QString &fileName);
0204 bool rename(const QString &oldName, const QString &newName);
0205 bool exists(const QString &name) const;
0206
0207 static QFileInfoList drives();
0208
0209 constexpr static inline QChar listSeparator() noexcept
0210 {
0211 #if defined(Q_OS_WIN)
0212 return u';';
0213 #else
0214 return u':';
0215 #endif
0216 }
0217
0218 static QChar separator()
0219 {
0220 #if defined(Q_OS_WIN)
0221 return u'\\';
0222 #else
0223 return u'/';
0224 #endif
0225 }
0226
0227 static bool setCurrent(const QString &path);
0228 static inline QDir current() { return QDir(currentPath()); }
0229 static QString currentPath();
0230
0231 static inline QDir home() { return QDir(homePath()); }
0232 static QString homePath();
0233 static inline QDir root() { return QDir(rootPath()); }
0234 static QString rootPath();
0235 static inline QDir temp() { return QDir(tempPath()); }
0236 static QString tempPath();
0237
0238 #if QT_CONFIG(regularexpression)
0239 static bool match(const QStringList &filters, const QString &fileName);
0240 static bool match(const QString &filter, const QString &fileName);
0241 #endif
0242
0243 static QString cleanPath(const QString &path);
0244 void refresh() const;
0245
0246 protected:
0247 explicit QDir(QDirPrivate &d);
0248
0249 QSharedDataPointer<QDirPrivate> d_ptr;
0250
0251 private:
0252 friend Q_CORE_EXPORT bool comparesEqual(const QDir &lhs, const QDir &rhs);
0253 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QDir)
0254 friend class QDirIterator;
0255 friend class QDirListing;
0256 friend class QDirListingPrivate;
0257
0258 QDirPrivate *d_func();
0259 const QDirPrivate *d_func() const { return d_ptr.constData(); }
0260 };
0261
0262 Q_DECLARE_SHARED(QDir)
0263 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
0264 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
0265
0266 #ifndef QT_NO_DEBUG_STREAM
0267 class QDebug;
0268 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
0269 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
0270 #endif
0271
0272 QT_END_NAMESPACE
0273
0274 #endif