Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDir::isWritable()?
Forum Update on Monday, May 27th 2025

QDir::isWritable()?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 20.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    szh1
    wrote on last edited by
    #1

    Why is there no function QDir::isWritable()? Is there another way to find out if a directory is writable?

    1 Reply Last reply
    1
    • I Offline
      I Offline
      iunknwn
      wrote on last edited by
      #2

      @QFileInfo fi(path);

      if(fi.isDir() && fi.isWritable()){
      qDebug() << "Voila";
      }else{
      qDebug() << "Can't write! sucks";
      }@

      Vista x64 with Qt 4.8.2 and VS2010

      1 Reply Last reply
      0
      • S Offline
        S Offline
        szh1
        wrote on last edited by
        #3

        On Windows, this returns false even on my home folder!?

        Is there another way?

        1 Reply Last reply
        0
        • I Offline
          I Offline
          iunknwn
          wrote on last edited by
          #4

          @#include <QtGui/QApplication>
          #include <QtCore/QFileInfo>
          #include <QtCore/QDebug>
          #include <QtCore/QDir>

          int main(int argc, char* argv[])
          {
          QApplication app(argc, argv);
          QFileInfo fi(QDir::homePath());
          if (fi.isDir() && fi.isWritable()){
          qDebug() << "voila";
          }else{
          qDebug() << "not so good";
          }
          app.exec();
          }@

          For me this whole indeed printed "voila"

          Vista x64 with Qt 4.8.2 and VS2010

          1 Reply Last reply
          0
          • I Offline
            I Offline
            iunknwn
            wrote on last edited by
            #5

            By the way, I'm on Vista x64 with Qt 4.6.1 and VS2008.

            Vista x64 with Qt 4.8.2 and VS2010

            1 Reply Last reply
            0
            • S Offline
              S Offline
              szh1
              wrote on last edited by
              #6

              I tried this and it worked. I must be doing something else wrong in my original program, but I just can't figure out what!

              My code is like this:

              @
              QFileInfo fi(QFileInfo(outputDir).path());
              if (!fi.isWritable())
              {
              QMessageBox::warning(NULL, "Error", "You must have write permission to the output directory.", QMessageBox::Ok, NULL);
              }
              @

              On Linux it works, but on windows I get the error message every time.

              1 Reply Last reply
              1
              • I Offline
                I Offline
                iunknwn
                wrote on last edited by
                #7

                @QFileInfo fi(QFileInfo(outputDir).path());@

                Regarding above line from your code.

                If outputDir="/foo/bar/blah"; then your code is not checking whether blah is writable. It is checking bar. Because your fi will be pointing to foo/bar and not foo/bar/blah.

                Vista x64 with Qt 4.8.2 and VS2010

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  iunknwn
                  wrote on last edited by
                  #8

                  Look at the documentation for path()

                  "http://doc.qt.nokia.com/4.6/qfileinfo.html#path":http://doc.qt.nokia.com/4.6/qfileinfo.html#path

                  Vista x64 with Qt 4.8.2 and VS2010

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    szh1
                    wrote on last edited by
                    #9

                    I tried adding a "/" and got the same results.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      iunknwn
                      wrote on last edited by
                      #10

                      What I'm trying to point out is that why can't you do
                      @
                      QFileInfo fi(outputDir) vs

                      QFileInfo fi(QFileInfo(outputDir).path()) ?@

                      Vista x64 with Qt 4.8.2 and VS2010

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        szh1
                        wrote on last edited by
                        #11

                        I'm sorry, I made a mistake. I meant outputPath (a directory + filename), not outputDir (just a directory).

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          iunknwn
                          wrote on last edited by
                          #12

                          Well my only suggestion is to print out all the variables step by step and check the return values of isValid(), isDir(), exists(), isReadable(), etc... and try to figure it out.

                          I don't think you are doing anything wrong.

                          Vista x64 with Qt 4.8.2 and VS2010

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            szh1
                            wrote on last edited by
                            #13

                            OK. Thanks for the help.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              szh1
                              wrote on last edited by
                              #14

                              I know I started this thread a long time ago, but I just stumbled over the answer while browsing the documentation for QFile. There, it says, (http://doc.qt.nokia.com/4.7/qfile.html#platform-specific-issues)

                              "File permissions are handled differently on Linux/Mac OS X and Windows. In a non writable directory on Linux, files cannot be created. This is not always the case on Windows, where, for instance, the 'My Documents' directory usually is not writable, but it is still possible to create files in it."

                              It appears that it really is not writable, but I could create files in it.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved