3

QFile has a static function

bool QFile::remove ( const QString & fileName ) [static]

which deletes the specified file. This is handy: we have a path to file, and we use a command to remove it. However, QDir does not have such command, only this one:

bool QDir::rmdir ( const QString & dirName ) const

which is non-static and hence demands an object. So I'm forced to do ugly things like

QDir().rmdir(path)

This is disgusting. Am I missing something?

ScumCoder
  • 690
  • 1
  • 8
  • 20
  • 3
    Note that for similar operations on QFile (copy, rename, remove), creating an object is the only way to get error details (errorString()) in case the function returns false, thus creating an object I consider best practice there. QDir is missing errorString(), sadly. Ideally it would be added though. – Frank Osterfeld Dec 13 '13 at 08:18
  • 3
    What that static `QFile::remove()` function does, is it creates a `QFile` object just like you do with `QDir`: [`QFile(fileName).remove();`](https://qt.gitorious.org/qt/qt/source/57756e72adf2081137b97f0e689dd16c770d10b1:src/corelib/io/qfile.cpp#L674). – thuga Dec 13 '13 at 08:36

1 Answers1

0

You can derive the class QDir and add your static method.

 class MyQDir : public QDir {
      // Define constructors/destructor

      static bool remove ( const QString & dirName ) {
            return QDir().rmdir(dirName);
      }
 };
Marcus
  • 275
  • 1
  • 12