一、前言

磁碟容量統計控制元件,說白了,就是用來統計本地碟符佔用的容量,包括但不限於已用空間、剩餘空間、總大小、已用百分比等,其中對應的百分比採用進度條顯示,該進度條的前景色和背景色及文字顏色可以設定,在整體換膚的時候就需要用到。 本控制元件的基本上沒有難點可言,就是相容WIN和LINUX作業系統,在WIN上採用winapi去讀取,linux採用QProcess去執行對應的命令(df -h)獲取結果,然後定時器執行,關聯訊號槽獲取返回的額資料解析即可,控制元件的應用場景主要是在一些嵌入式裝置上面,方便使用者檢視當前還剩餘多少空間。

主要功能: 1。 可自動載入本地儲存裝置的總容量/已用容量 2。 進度條顯示已用容量 3。 支援所有作業系統 4。 增加隨身碟或者SD卡到達訊號

二、程式碼思路

void

DeviceSizeTable

::

load

()

{

//清空原有資料

int

row

=

this

->

rowCount

();

for

int

i

=

0

i

<

row

i

++

{

this

->

removeRow

0

);

}

#ifdef Q_OS_WIN

QFileInfoList

list

=

QDir

::

drives

();

foreach

QFileInfo

dir

list

{

QString

dirName

=

dir

absolutePath

();

LPCWSTR

lpcwstrDriver

=

LPCWSTR

dirName

utf16

();

ULARGE_INTEGER

liFreeBytesAvailable

liTotalBytes

liTotalFreeBytes

if

GetDiskFreeSpaceEx

lpcwstrDriver

&

liFreeBytesAvailable

&

liTotalBytes

&

liTotalFreeBytes

))

{

QString

use

=

QString

::

number

((

double

)(

liTotalBytes

QuadPart

-

liTotalFreeBytes

QuadPart

/

GB

‘f’

1

);

use

+=

“G”

QString

free

=

QString

::

number

((

double

liTotalFreeBytes

QuadPart

/

GB

‘f’

1

);

free

+=

“G”

QString

all

=

QString

::

number

((

double

liTotalBytes

QuadPart

/

GB

‘f’

1

);

all

+=

“G”

int

percent

=

100

-

((

double

liTotalFreeBytes

QuadPart

/

liTotalBytes

QuadPart

*

100

insertSize

dirName

use

free

all

percent

);

}

}

#else

process

->

start

“df -h”

);

#endif

}

void

DeviceSizeTable

::

readData

()

{

while

process

->

atEnd

())

{

QString

result

=

QLatin1String

process

->

readLine

());

#ifdef __arm__

if

result

startsWith

“/dev/root”

))

{

checkSize

result

“本地儲存”

);

}

else

if

result

startsWith

“/dev/mmcblk”

))

{

checkSize

result

“本地儲存”

);

}

else

if

result

startsWith

“/dev/mmcblk1p”

))

{

checkSize

result

“SD卡”

);

QStringList

list

=

result

split

“ ”

);

emit

sdcardReceive

list

at

0

));

}

else

if

result

startsWith

“/dev/sd”

))

{

checkSize

result

“隨身碟”

);

QStringList

list

=

result

split

“ ”

);

emit

udiskReceive

list

at

0

));

}

#else

if

result

startsWith

“/dev/sd”

))

{

checkSize

result

“”

);

QStringList

list

=

result

split

“ ”

);

emit

udiskReceive

list

at

0

));

}

#endif

}

}

void

DeviceSizeTable

::

checkSize

const

QString

&

result

const

QString

&

name

{

QString

dev

use

free

all

int

percent

=

0

QStringList

list

=

result

split

“ ”

);

int

index

=

0

for

int

i

=

0

i

<

list

count

();

i

++

{

QString

s

=

list

at

i

)。

trimmed

();

if

s

==

“”

{

continue

}

index

++

if

index

==

1

{

dev

=

s

}

else

if

index

==

2

{

all

=

s

}

else

if

index

==

3

{

use

=

s

}

else

if

index

==

4

{

free

=

s

}

else

if

index

==

5

{

percent

=

s

left

s

length

()

-

1

)。

toInt

();

break

}

}

if

name

length

()

>

0

{

dev

=

name

}

insertSize

dev

use

free

all

percent

);

}

void

DeviceSizeTable

::

insertSize

const

QString

&

name

const

QString

&

use

const

QString

&

free

const

QString

&

all

int

percent

{

int

row

=

this

->

rowCount

();

this

->

insertRow

row

);

QTableWidgetItem

*

itemname

=

new

QTableWidgetItem

name

);

QTableWidgetItem

*

itemuse

=

new

QTableWidgetItem

use

);

itemuse

->

setTextAlignment

Qt

::

AlignCenter

);

QTableWidgetItem

*

itemfree

=

new

QTableWidgetItem

free

);

itemfree

->

setTextAlignment

Qt

::

AlignCenter

);

QTableWidgetItem

*

itemall

=

new

QTableWidgetItem

all

);

itemall

->

setTextAlignment

Qt

::

AlignCenter

);

this

->

setItem

row

0

itemname

);

this

->

setItem

row

1

itemuse

);

this

->

setItem

row

2

itemfree

);

this

->

setItem

row

3

itemall

);

QProgressBar

*

bar

=

new

QProgressBar

bar

->

setRange

0

100

);

bar

->

setValue

percent

);

QString

qss

=

QString

“QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}”

“QProgressBar::chunk{border-radius:0px;}”

)。

arg

bgColor

name

());

if

percent

<

50

{

qss

+=

QString

“QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}”

)。

arg

textColor1

name

())。

arg

chunkColor1

name

());

}

else

if

percent

<

90

{

qss

+=

QString

“QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}”

)。

arg

textColor2

name

())。

arg

chunkColor2

name

());

}

else

{

qss

+=

QString

“QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}”

)。

arg

textColor3

name

())。

arg

chunkColor3

name

());

}

bar

->

setStyleSheet

qss

);

this

->

setCellWidget

row

4

bar

);

}

三、效果圖

Qt開源作品12-硬碟容量控制元件

四、開源主頁

以上作品完整原始碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。

1。 國內站點:

https://

gitee。com/feiyangqingyu

n/QWidgetDemo

2。 國際站點:

https://

github。com/feiyangqingy

un/QWidgetDemo

3。 個人主頁:

https://

blog。csdn。net/feiyangqi

ngyun

4。 知乎主頁:

https://www。

zhihu。com/people/feiyan

gqingyun/