使用QFile创建包含目录的文件

tob*_*obi 7 qt

我想知道是否有可能一次性创建带有目录的文件.例如,我想创建文件脚本/ myFile.txt.

我写了这样的代码:

QFile _file( path );
QDir _dir;

// check if "scripts" folder exists
int _dirExists = _dir.exists( "scripts" );
// if not, create it
if( !_dirExists )
    _dir.mkdir( "scripts" );

// open file in write mode (and text mode) 
int _fileOpened = _file.open( QIODevice::WriteOnly | QIODevice::Text );
if( !_fileOpened ) {
// ...
Run Code Online (Sandbox Code Playgroud)

但我不得不使用QDir类,我不喜欢它的样子.我无法理解为什么QFile不像大多数这种框架那样创建必要的目录.或许我错过了什么?

rha*_*iso 10

我知道它的几年后,但QDir :: mkpath刚刚为我工作.

http://qt-project.org/doc/qt-4.8/qdir.html#mkpath


bma*_*anc 6

不,我相信您无法创建该文件,并且文件包含其中的目录。