I'm making a program which copies files in Qt. I want to know how can I use QProgressBar
with bool QFile::copy(const QString & fileName, const QString & newName)
. Is this even possible with copy
function? Can the process of copying be paused?
问题:
回答1:
You can't do this using the static QFile::copy() method.
As Maciej stated before you need to write your own class. It should use two QFile objects, one for reading one for writing. Transfer the data in portions (e.g 1% of the entire file size) and emit a progress signal after each portion. You can connect this signal to a progress dialog.
If you need this to work in the background you should implement it using a QThread.
First try to decide if you need a class that does the copy work asynchonously (without blocking the GUI) or synchronously (blocking the GUI). The latter is easier to programming but most times not what is intended (e.g. you can not cancel or pause a copy operation by button click if the GUI is blocked).
You can have a look here for a pretty extensive Qt 4 class: http://docs.huihoo.com/qt/solutions/4/qtcopydialog/qtfilecopier.html but I am not sure if this will help due to its complexity.
回答2:
Check out the class that QFile
derives from QIODevice there is a signal bytesWritten()
connect to it and stat the file size via QFileInfo(fromFile).size()
to get a read how to use signals check my other answer