Get free disk space
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:
(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
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
I tried to use qtmobility, but qtcreator was not unable to find the headers.
Using the windows API fixed the problem.
the windows API requires a WCHAR. Use QString::toWcharArray()
to convert a QString to WCHAR.
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.
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.
or use QtSystems module's "QStorageInfo":http://qt.gitorious.org/qt/qtsystems/blobs/dev/src/systeminfo/qstorageinfo.h class
QStorageInfo is good! However i am not able to use it. I added in my project
But when compiling my project i get:
no such file or directory QtSystemInfo/qsysteminfoglobal.h
It's not part of the official Qt package. It's a separate module that you need to build yourself.
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.
Had you installed perl ?
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
Just test is in a simple => main.cpp or is in a simple class to test it
wchar_t * pszDrive = L"e:\"; // here you can change the letter of your drive.
fResult = GetDiskFreeSpaceEx (pszDrive,
printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
printf ("Available space to caller = %I64u MB\n",
i64FreeBytesToCaller) / (10241024));
printf ("Total space = %I64u MB\n",
i64TotalBytes) / (10241024));
printf ("Free space on drive = %I64u MB\n",
i64FreeBytes) / (1024*1024));
printf ("error: %lu: could not get free space for "%s"\n",
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