Qt:如何创建一个文件(其路径需要目录)

Qt: how to create a file (with directorys required by its path)?

本文关键字:文件 路径 一个 何创建 Qt 创建      更新时间:2023-10-16

Qt:如何创建一个包含路径所需目录的文件?如果它不存在,则说创建"./gg/ggg./fff/ff/file.dd"?

您可以将路径设置为

QDir directory("gg/ggg./fff/ff/file");

然后,您可以创建一个文件夹,例如

QDir().mkdir("MyFolder");

bool QDir::mkdir ( const QString & dirName ) const
Creates a sub-directory called dirName.
Returns true on success; otherwise returns false.
See also rmdir().

请参阅 http://doc.qt.io/qt-5/qdir.html#mkdir

bool QDir::mkpath ( const QString & dirPath ) const
Creates the directory path dirPath.
The function will create all parent directories necessary to create the directory.
Returns true if successful; otherwise returns false.

请参阅 http://doc.qt.io/qt-5/qdir.html#mkpath

bool QFile::open ( OpenMode mode )   [virtual]
Reimplemented from QIODevice::open().
Opens the file using OpenMode mode, returning true if successful; otherwise false.
In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

请参阅 http://doc.qt.io/qt-5/qfile.html#open