Qt – Creating file with directories using QFile

qt

I wonder if it's possible to create file with its directories at one blow. For example I want to create file scripts/myFile.txt.

I have written such code:

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 ) {
// ...

but I had to use QDir class and I don't like how it looks like. I can't understand why the QFile doesn't create necessary directories itself like in most of this kind of frameworks. Or maybe I have missed something?

Best Answer

I know its years afterwars, but QDir::mkpath just worked worked for me.

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

Related Topic