这篇文章主要介绍了Qt如何获取电脑磁盘容量的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Qt如何获取电脑磁盘容量文章都会有所收获,下面我们一起来看看吧。项目中用到了监测某磁盘(如:C盘、D盘等)的总容量和可用容
这篇文章主要介绍了Qt如何获取电脑磁盘容量的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Qt如何获取电脑磁盘容量文章都会有所收获,下面我们一起来看看吧。
项目中用到了监测某磁盘(如:C盘、D盘等)的总容量和可用容量,查了一下,发现其实挺简单,调用一个windows自带的api:GetDiskFreeSpaceEx( ... )就可以解决。用Qt做了一个测试例子,如下所示。
另:测试发现,如果给定路径不存在(如程序中"F:\"),则获取不到信息;通常给定用以获取磁盘容量信息的路径为“C:\”、"D:\"等,但给定已存在路径(如程序中"D:\Test"),同样可以获取到该路径所属信息。
示例代码:
#include "acquirediskspace.h"#include <QtWidgets/QApplication> int main(int arGC, char *argv[]){ QApplication a(argc, argv); AcquiRediskSpace w; w.show(); return a.exec();}
#ifndef ACQUIREDISKSPACE_H#define ACQUIREDISKSPACE_H #include <QDialog> class QLineEdit; class AcquireDiskSpace : public QDialog{ Q_OBJECT public: AcquireDiskSpace( QWidget *parent = 0 ); ~AcquireDiskSpace(); protected slots: void slotDiskChanged( const QString& ); private: void GetDiskSpace( QString strDisk, double& dTotalBytes, double& dFreeBytes ); private: QLineEdit* m_pEdtTotalSpace; QLineEdit* m_pEdtFreeSpace;}; #endif // ACQUIREDISKSPACE_H
#include "acquirediskspace.h" #include <QLabel>#include <QLineEdit>#include <QPushButton>#include <QComboBox>#include <QGridLayout>#include <QMessageBox> #include "windows.h" AcquireDiskSpace::AcquireDiskSpace( QWidget *parent ) : QDialog( parent ){ setWindowFlags( Qt::WindowTitleHint ); resize( 400, 400 ); setStyleSheet( "QDialog{background:white;}" ); m_pEdtTotalSpace = new QLineEdit; m_pEdtTotalSpace->setEnabled( false ); m_pEdtTotalSpace->setMinimumHeight( 30 ); m_pEdtFreeSpace = new QLineEdit; m_pEdtFreeSpace->setEnabled( false ); m_pEdtFreeSpace->setMinimumHeight( 30 ); //Disk QComboBox* pCmbDisk = new QComboBox; pCmbDisk->addItem( "C:/" ); pCmbDisk->addItem( "D:/Test" ); pCmbDisk->addItem( "E:/" ); pCmbDisk->addItem( "F:/" ); pCmbDisk->addItem( "G:/" ); pCmbDisk->setMinimumHeight( 30 ); //Button QPushButton* pBtnOK = new QPushButton( "OK" ); pBtnOK->setMinimumHeight( 30 ); QHBoxLayout* pBtnLayout = new QHBoxLayout; pBtnLayout->addWidget( pBtnOK ); //MainLayout QGridLayout* pMainLayout = new QGridLayout; pMainLayout->addWidget( new QLabel( "Total:" ), 0, 0, Qt::AlignRight ); pMainLayout->addWidget( m_pEdtTotalSpace, 0, 1 ); pMainLayout->addWidget( new QLabel( "GB" ), 0, 2 ); pMainLayout->addWidget( new QLabel( "Free:" ), 1, 0, Qt::AlignRight ); pMainLayout->addWidget( m_pEdtFreeSpace, 1, 1 ); pMainLayout->addWidget( new QLabel( "GB" ), 1, 2 ); pMainLayout->addWidget( new QLabel( "Disk:" ), 2, 0, Qt::AlignRight ); pMainLayout->addWidget( pCmbDisk, 2, 1 ); pMainLayout->addLayout( pBtnLayout, 3, 1 ); setLayout( pMainLayout ); //connects connect( pCmbDisk, SIGNAL( currentIndexChanged( const QString& ) ), this, SLOT( slotDiskChanged( const QString& ) ) ); connect( pBtnOK, SIGNAL( clicked() ), this, SLOT( accept() ) ); //Init QString strDisk = pCmbDisk->currentText(); slotDiskChanged( strDisk );} AcquireDiskSpace::~AcquireDiskSpace(){ } void AcquireDiskSpace::slotDiskChanged( const QString& strDisk ){ double dTotalBytes, dFreeBytes; GetDiskSpace( strDisk, dTotalBytes, dFreeBytes ); m_pEdtTotalSpace->setText( QString::number( dTotalBytes, 'f', 3 ) ); m_pEdtFreeSpace->setText( QString::number( dFreeBytes, 'f', 3 ) );} void AcquireDiskSpace::GetDiskSpace( QString strDisk, double& dTotalBytes, double& dFreeBytes ){ LPCWSTR lpcwstrDriver = (LPCWSTR)strDisk.utf16(); ULARGE_INTEGER lFreeBytesAvailable, lTotalBytesTemp, lTotalFreeBytes; if( !GetDiskFreeSpaceEx( lpcwstrDriver, &lFreeBytesAvailable, &lTotalBytesTemp, &lTotalFreeBytes ) ) { QMessageBox::warning( 0, "Warning", "Acquire Disk Space Failed !" ); dTotalBytes = -1; dFreeBytes = -1; return; } //unit : GB dTotalBytes = lTotalBytesTemp.QuadPart / 1024.0 / 1024 / 1024; dFreeBytes = lTotalFreeBytes.QuadPart / 1024.0 / 1024 / 1024;}
结果如下:
(1)C盘:
(2)D盘:
(3)F盘(F盘实际上并不存在,故获取不到,给了默认值-1,-1)
关于“Qt如何获取电脑磁盘容量”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Qt如何获取电脑磁盘容量”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。
--结束END--
本文标题: Qt如何获取电脑磁盘容量
本文链接: https://lsjlt.com/news/357544.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0