檔案/或目錄拷貝,在子執行緒中實現,可實時通知拷貝進度
#ifndef systemfilecopy_h
#define systemfilecopy_h
#include
class
qthread
;class
systemfilecopy
:public qobject
void
startthread()
;protected slots:
void
startcopy()
;signals:
void
sig_copyprogress
(qint64 bytescopyed, qint64 total)
;//拷貝進度
void
sig_startcopy
(const qstring& filename)
;//單個檔案開始拷貝
void
sig_finshedcopy
(const qstring& targetpath,
bool result)
;//單個檔案拷貝結束訊號
void
sig_errorinfo
(const qstring& errorstring)
;//錯誤資訊
void
sig_finished
(bool bsuccess)
;//拷貝結束訊號
private
:bool
detailfileinfo
(const qstring& from,
const qstring& target, qstringlist& fromfilelist, qstringlist& tofilelist)
;private
: qstring m_ssourcepath;
//源目錄/檔案
qstring m_stargetpath;
//目標目錄
qthread *m_pthread;
bool m_binterrupt;
//是否中斷取消};
#endif
// systemfilecopy_h
#include
"systemfilecopy.h"
#include
#include
#include
#include
#define copy_size 1024*1024*4
systemfilecopy::
systemfilecopy
(const qstring& sourcepath,
const qstring& targetpath)
:m_ssourcepath
(sourcepath)
,m_stargetpath
(targetpath)
,m_pthread
(new qthread)
,m_binterrupt
(false
)void systemfilecopy::
startcopy()
int nsize = fromlist.
size()
;if(nsize != targetlist.
size()
|| nsize ==0)
qint64 totalsize =
0, writetotal =0;
foreach (
const qstring& var, fromlist)
for(
int i =
0; i < nsize;
++i)
qfile sourcefile
(fromlist.
at(i));
if(!sourcefile.
open
(qiodevice::readonly)
) qfile targetfile
(targetpath);if
(qfile::
exists
(targetpath)
) qfile::
remove
(targetpath);if
(!targetfile.
open
(qiodevice::writeonly)
) emit sig_startcopy
(sourcefile.
filename()
);qint64 sourcesize = sourcefile.
size()
;while
(sourcesize)
qbytearray bytearry = sourcefile.
read
(copy_size)
; qint64 writesize = targetfile.
write
(bytearry);if
(bytearry.
size()
== writesize)
} sourcefile.
close()
; targetfile.
close()
; emit sig_finshedcopy
(targetpath,
true);
} emit sig_finished
(true);
}void systemfilecopy::
startthread()
bool systemfilecopy::
detailfileinfo
(const qstring& from,
const qstring& target, qstringlist& fromfilelist, qstringlist& tofilelist)
else}}
else
return
true;}
``使用方式:
```cpp
systemfilecopy* pcopy =
newsystemfilecopy
("d:/abc"
,"d:/ded");
pcopy-
>
startthread()
;
類中有訊號,可根據需要自行連線 帶進度條的檔案拷貝
帶進度條的檔案拷貝 可以拷貝任何檔案,拷貝過程有進度條實時反映,並記錄使用時間.顯示效果如圖 private void btncopyfile click object sender,system.eventargs e if false checkfilepath cfw.sourcefile t...
Qt之QProgressBar進度條
值描述 qprogressbar toptobottom 0文字是順時針旋轉了90度 qprogressbar bottomtotop 1文字是逆時針旋轉90度 qprogressbar pprogressbar new qprogressbar this pprogressbar setorien...
QT進度條簡單實現
話不多說,開始步驟 五步曲 下面有效果git圖 第一步 往布局中拉乙個progress bar 和 pushbutton 第二步 右鍵pushbutton按鈕轉到槽函式 第三步 直接上 吧!h ifndef mainwindow h define mainwindow h include inclu...