sharing everything

Qt Remove Directory

Qt Remove Directory

Qt have rich QDir class provides access to directory structures and their contents. QDir have function QDir::rmdir to remove directory. The directory must be empty for rmdir() to succeed. This Qt Remove Directory function Returns true if successful,  otherwise returns false. The problem is how Qt Remove Directory if that directory not empty? If you have non empty directory, you can use this Qt Remove Directory function to remove non empty directory with all subdirectory. This is a Qt Remove Directory function :

You can call this Qt Remove Directory function with this method :

If you have any question or method about this Qt Remove Directory, please share to us.

Source :
My name is Toto Sugito. This is my notes when I try something. Maybe, this is NOT THE BEST SOLUTION for your problems. If you have other method or idea that better with my post, please share in this blog. Thank for visiting my site.


  1. AnCoward AnCoward
    November 6, 2011    

    I think, you should use QFile::remove( path ), otherwise looks fine to me.

    • November 7, 2011    

      Hi AnCoward,

      Thank you for your comment. I think we can not use your method. I use your method and fail. The problem for this posting is how to remove not empty directory.

      This is like how to remove directory with sub directory and every directory is not empty. I am figured like this :
      We want to remove directory ‘a’ with all subdirectory and file in a.

      If you read QFile::remove( path ) documentation :
      bool QFile::remove ( const QString & fileName ) [static]
      This is an overloaded function.
      Removes the file specified by the fileName given.

      So, this function is to remove a file, not directory. Thank you for visiting my site

      • AnCoward AnCoward
        November 7, 2011    

        I meant instead of:
        QFile file( path );
        if ( ! file.remove() )
        I’d use static method:
        if ( ! QFile::remove( path ) )

        But whatever, doesn’t really matter.

        I’m actually surprised that there is no recursive remove in QDir, people have to implement it for themselves every time.
        Yet another version in KDE sources:

        > Thank you for visiting my site
        You’re welcome, found this post in Google+ sparks.

  2. Kristian Kristian
    April 6, 2012    

    Thank you! Your code works fine except that it does not handle hidden directories (very common under UNIX as directories starting with a dot). If you append QDir::Hidden to the list of flags then it handles these too :)

    • April 10, 2012    

      Hi Kristian,
      Thank you for visiting my site and give other idea about how to remove hidden Dir in Unix/Linux :)

Leave a Reply

Your email address will not be published. Required fields are marked *



Bloggers - Meet Millions of Bloggers