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. Get free disk space
Forum Updated to NodeBB v4.3 + New Features

Get free disk space

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 11.2k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mar91
    wrote on last edited by
    #1

    Hello,
    i am looking for a function to get free disk space on a selected volume, but i can't find none native.
    So i found this:
    http://stackoverflow.com/questions/1732717/how-to-determine-how-much-free-space-on-a-drive-in-qt
    (ticked answer). I like the solution, because the application will run on windows. But what headers do i have to add? What lines in the .pro ? Thank you!
    This is the API http://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      There's a "QtMobility::QSystemStorageInfo":http://doc.qt.digia.com/qtmobility/qsystemstorageinfo.html#totalDiskSpace class for that if you're willing to use QtMobility.

      As for the Windows api there's always info about what headers and libs the function lives in at the bottom of the doc.
      In your case it says:

      Header: FileAPI.h (include Windows.h); so you include Windows.h somewhere where you need the api.

      Library: Kernel32.lib so you add LIBS += -lkernel32 in your .pro file

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mar91
        wrote on last edited by
        #3

        I tried to use qtmobility, but qtcreator was not unable to find the headers.
        Using the windows API fixed the problem.
        NOTE:
        the windows API requires a WCHAR. Use QString::toWcharArray()
        to convert a QString to WCHAR.

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sometimes when I need to quickly pass a QString into the windows apis I use mystr.toStdWString().c_str(). Saves me writing the two lines to create an array. Of course the pointer becomes invalid right after the call so don't store it, but it can be safely passed into the function.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            Have a look at "this thread":http://qt-project.org/forums/viewthread/27523/. Aside from "QtMobility::QSystemStorageInfo":http://doc.qt.digia.com/qtmobility/qsystemstorageinfo.html#totalDiskSpace you could also use "boost:filesystem":http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/index.htm

            @
            boost::filesystem::path p (filepath);
            boost::filesystem::space_info s = boost::filesystem::space(p);
            quint64 uFreeStorage = s.available;
            quint64 uTotalStorage = s.capacity;
            @

            And then of course there are also the native functions, but I would always prefer a platform independent solution.

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              or use QtSystems module's "QStorageInfo":http://qt.gitorious.org/qt/qtsystems/blobs/dev/src/systeminfo/qstorageinfo.h class

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mar91
                wrote on last edited by
                #7

                QStorageInfo is good! However i am not able to use it. I added in my project
                #include "systeminfo/qstorageinfo.h"

                But when compiling my project i get:
                no such file or directory QtSystemInfo/qsysteminfoglobal.h

                1 Reply Last reply
                0
                • Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It's not part of the official Qt package. It's a separate module that you need to build yourself.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mar91
                    wrote on last edited by
                    #9

                    Ok , i got the complete package, and opened the qtsystem.pro
                    Configured to build with qt 5.0.2 and runned qmake:
                    Failed to run: C:\Qt\Qt5.0.2\5.0.2\mingw47_32\bin\syncqt.bat -module QtPublishSubscribe -version 5.2.0 -outdir C:/Users/Marco/Desktop/build-qtsystems-Desktop_Qt_5_0_2_MinGW_32bit-Debug C:/Users/Marco/Desktop/qt-qtsystems

                    I can't understand how to fix this.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dbzhang800
                      wrote on last edited by
                      #10

                      Had you installed perl ?

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        domzique
                        wrote on last edited by
                        #11

                        Hi,
                        this is a simple code to find out :

                        • the total space of a disk
                        • the freespace of a disk

                        I am not using boost or other Qt modules. I am using MSDN features.

                        You need to include
                        @
                        #include <windows.h>
                        #include <stdio.h>
                        #include <stdlib.h>
                        @

                        Just test is in a simple => main.cpp or is in a simple class to test it

                        @
                        BOOL fResult;

                        wchar_t * pszDrive = L"e:\"; // here you can change the letter of your drive.

                        qint64 i64FreeBytesToCaller,
                        i64TotalBytes,
                        i64FreeBytes;

                        fResult = GetDiskFreeSpaceEx (pszDrive,
                        (PULARGE_INTEGER)&i64FreeBytesToCaller,
                        (PULARGE_INTEGER)&i64TotalBytes,
                        (PULARGE_INTEGER)&i64FreeBytes);
                        if (fResult)
                        {
                        printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
                        printf ("Available space to caller = %I64u MB\n",
                        i64FreeBytesToCaller) / (10241024));
                        printf ("Total space = %I64u MB\n",
                        i64TotalBytes) / (1024
                        1024));
                        printf ("Free space on drive = %I64u MB\n",
                        i64FreeBytes) / (1024*1024));
                        }

                        if (!fResult)
                        printf ("error: %lu: could not get free space for "%s"\n",
                        GetLastError(), argv[1]);
                        @

                        if you want to use a QString for the letter of the drive, just do those two conversions :

                        @
                        // m_diskLetter is a QString
                        std::wstring ws = m_diskLetter->toStdWString();
                        const wchar_t * pszDrive = ws.c_str(); // pszDrive is the letter of the drive for the fonction
                        @

                        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