91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Qt獲取電腦磁盤容量

發布時間:2020-07-07 05:18:49 來源:網絡 閱讀:2435 作者:006玩命 欄目:編程語言

??項目中用到了監測某磁盤(如:C盤、D盤等)的總容量和可用容量,查了一下,發現其實挺簡單,調用一個Windows自帶的API:GetDiskFreeSpaceEx( ... )就可以解決。用Qt做了一個測試例子,如下所示。
??另:測試發現,如果給定路徑不存在(如程序中"F:\"),則獲取不到信息;通常給定用以獲取磁盤容量信息的路徑為“C:\”、"D:\"等,但給定已存在路徑(如程序中"D:\Test"),同樣可以獲取到該路徑所屬信息。


示例代碼:

/*main.cpp*/

#include "acquirediskspace.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    AcquireDiskSpace w;
    w.show();
    return a.exec();
}
/*acquirediskspace.h */

#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
/*acquirediskspace.cpp */

#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盤:
??Qt獲取電腦磁盤容量

(2)D盤:
??Qt獲取電腦磁盤容量

(3)F盤(F盤實際上并不存在,故獲取不到,給了默認值-1,-1)
??Qt獲取電腦磁盤容量Qt獲取電腦磁盤容量

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

常德市| 宝清县| 通道| 阿尔山市| 合作市| 车致| 武定县| 自治县| 乌恰县| 伊吾县| 台中县| 东明县| 永修县| 青海省| 长岭县| 六枝特区| 罗山县| 浙江省| 伊通| 绍兴县| 卫辉市| 赤水市| 抚顺市| 保靖县| 延边| 延寿县| 福州市| 黄石市| 延长县| 商南县| 陵水| 锡林郭勒盟| 汕尾市| 于田县| 遵义市| 黄平县| 罗定市| 涡阳县| 武邑县| 旅游| 鹿邑县|