70class Q_CORE_EXPORT
QFile :
public QFileDevice
75 Q_DECLARE_PRIVATE(QFile)
81 QFILE_MAYBE_EXPLICIT QFile(
const std::filesystem::path &name);
82#elif QT_CONFIG(cxx17_filesystem)
83 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
84 QFILE_MAYBE_EXPLICIT QFile(
const T &name) : QFile(QtPrivate::fromFilesystemPath(name))
90 explicit QFile(QObject *parent);
91 QFile(
const QString &name, QObject *parent);
94 QFile(
const std::filesystem::path &path, QObject *parent);
95#elif QT_CONFIG(cxx17_filesystem)
96 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
97 QFile(
const T &path, QObject *parent) : QFile(QtPrivate::fromFilesystemPath(path), parent)
104 QString fileName()
const override;
105#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
106 std::filesystem::path filesystemFileName()
const
107 {
return QtPrivate::toFilesystemPath(fileName()); }
109 void setFileName(
const QString &name);
111 void setFileName(
const std::filesystem::path &name);
112#elif QT_CONFIG(cxx17_filesystem)
113 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
114 void setFileName(
const T &name)
116 setFileName(QtPrivate::fromFilesystemPath(name));
120#if defined(Q_OS_DARWIN)
122 static inline QByteArray encodeName(
const QString &fileName)
124 return fileName.normalized(QString::NormalizationForm_D).toUtf8();
126 static QString decodeName(
const QByteArray &localFileName)
129 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
131 static inline QString decodeName(
const char *localFileName)
133 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
136 static inline QByteArray encodeName(
const QString &fileName)
138 return fileName.toLocal8Bit();
140 static QString decodeName(
const QByteArray &localFileName)
142 return QString::fromLocal8Bit(localFileName);
144 static inline QString decodeName(
const char *localFileName)
146 return QString::fromLocal8Bit(localFileName);
151 static bool exists(
const QString &fileName);
153 static bool exists(
const std::filesystem::path &fileName);
154#elif QT_CONFIG(cxx17_filesystem)
155 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
156 static bool exists(
const T &fileName)
158 return exists(QtPrivate::fromFilesystemPath(fileName));
162 QString symLinkTarget()
const;
163 static QString symLinkTarget(
const QString &fileName);
165 std::filesystem::path filesystemSymLinkTarget()
const;
166 static std::filesystem::path filesystemSymLinkTarget(
const std::filesystem::path &fileName);
167#elif QT_CONFIG(cxx17_filesystem)
168 std::filesystem::path filesystemSymLinkTarget()
const
170 return QtPrivate::toFilesystemPath(symLinkTarget());
172 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
173 static std::filesystem::path filesystemSymLinkTarget(
const T &fileName)
175 return QtPrivate::toFilesystemPath(symLinkTarget(QtPrivate::fromFilesystemPath(fileName)));
180 static bool remove(
const QString &fileName);
182 static bool remove(
const std::filesystem::path &fileName);
183#elif QT_CONFIG(cxx17_filesystem)
184 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
185 static bool remove(
const T &fileName)
187 return remove(QtPrivate::fromFilesystemPath(fileName));
191 Q_DECL_PURE_FUNCTION
static bool supportsMoveToTrash();
193 static bool moveToTrash(
const QString &fileName, QString *pathInTrash =
nullptr);
195 static bool moveToTrash(
const std::filesystem::path &fileName, QString *pathInTrash =
nullptr);
196#elif QT_CONFIG(cxx17_filesystem)
197 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
198 static bool moveToTrash(
const T &fileName, QString *pathInTrash =
nullptr)
200 return moveToTrash(QtPrivate::fromFilesystemPath(fileName), pathInTrash);
204 bool rename(
const QString &newName);
205 static bool rename(
const QString &oldName,
const QString &newName);
207 bool rename(
const std::filesystem::path &newName);
208 static bool rename(
const std::filesystem::path &oldName,
209 const std::filesystem::path &newName);
210#elif QT_CONFIG(cxx17_filesystem)
211 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
212 bool rename(
const T &newName)
214 return rename(QtPrivate::fromFilesystemPath(newName));
216 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
217 static bool rename(
const T &oldName,
const T &newName)
219 return rename(QtPrivate::fromFilesystemPath(oldName),
220 QtPrivate::fromFilesystemPath(newName));
224 bool link(
const QString &newName);
225 static bool link(
const QString &fileName,
const QString &newName);
227 bool link(
const std::filesystem::path &newName);
228 static bool link(
const std::filesystem::path &fileName,
229 const std::filesystem::path &newName);
230#elif QT_CONFIG(cxx17_filesystem)
231 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
232 bool link(
const T &newName)
234 return link(QtPrivate::fromFilesystemPath(newName));
236 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
237 static bool link(
const T &fileName,
const T &newName)
239 return link(QtPrivate::fromFilesystemPath(fileName),
240 QtPrivate::fromFilesystemPath(newName));
244#if QT_CONFIG(temporaryfile)
245 bool copy(
const QString &newName, std::optional<QFileDevice::Permissions> perm = std::nullopt);
246 static bool copy(
const QString &fileName,
const QString &newName,
247 std::optional<QFileDevice::Permissions> perm = std::nullopt);
248# if QT_CORE_REMOVED_SINCE(6
, 12
)
249 bool copy(
const QString &newName);
250 static bool copy(
const QString &fileName,
const QString &newName);
254 bool copy(
const std::filesystem::path &newName);
255 static bool copy(
const std::filesystem::path &fileName,
256 const std::filesystem::path &newName);
257#elif QT_CONFIG(cxx17_filesystem) && QT_CONFIG(temporaryfile)
258 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
259 bool copy(
const T &newName)
261 return copy(QtPrivate::fromFilesystemPath(newName));
263 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
264 static bool copy(
const T &fileName,
const T &newName)
266 return copy(QtPrivate::fromFilesystemPath(fileName),
267 QtPrivate::fromFilesystemPath(newName));
271 QFILE_MAYBE_NODISCARD
bool open(OpenMode flags) override;
272 QFILE_MAYBE_NODISCARD
bool open(OpenMode flags, Permissions permissions);
273 QFILE_MAYBE_NODISCARD
bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
274 QFILE_MAYBE_NODISCARD
bool open(
int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
276 qint64 size()
const override;
278 bool resize(qint64 sz) override;
279 static bool resize(
const QString &filename, qint64 sz);
281 Permissions permissions()
const override;
282 static Permissions permissions(
const QString &filename);
283 bool setPermissions(Permissions permissionSpec) override;
284 static bool setPermissions(
const QString &filename, Permissions permissionSpec);
286 static Permissions permissions(
const std::filesystem::path &filename);
287 static bool setPermissions(
const std::filesystem::path &filename, Permissions permissionSpec);
288#elif QT_CONFIG(cxx17_filesystem)
289 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
290 static Permissions permissions(
const T &filename)
292 return permissions(QtPrivate::fromFilesystemPath(filename));
294 template<
typename T, QtPrivate::ForceFilesystemPath<T> = 0>
295 static bool setPermissions(
const T &filename, Permissions permissionSpec)
297 return setPermissions(QtPrivate::fromFilesystemPath(filename), permissionSpec);
303 QFile(QFilePrivate &dd);
305 QFile(QFilePrivate &dd, QObject *parent =
nullptr);
309 friend class QTemporaryFile;
310 Q_DISABLE_COPY(QFile)