QFile::copy() 的进度条?

2022-01-20 00:00:00 复制 qt progress-bar c++ qfile

我正在制作一个在 Qt 中复制文件的程序.我想知道如何将 QProgressBarbool QFile::copy(const QString & fileName, const QString & newName) 一起使用.这甚至可以通过 copy 功能实现吗?复制过程可以暂停吗?

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?

推荐答案

使用静态 QFile::copy() 方法无法做到这一点.

You can't do this using the static QFile::copy() method.

正如 Maciej 在您需要编写自己的课程之前所说的那样.它应该使用两个 QFile 对象,一个用于读取,一个用于写入.分部分传输数据(例如,整个文件大小的 1%)并在每个部分之后发出进度信号.您可以将此信号连接到进度对话框.

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.

如果你需要它在后台工作,你应该使用 QThread 来实现它.

If you need this to work in the background you should implement it using a QThread.

首先尝试确定您是否需要一个异步(不阻塞 GUI)或同步(阻塞 GUI)复制工作的类.后者更容易编程,但大多数时候不是预期的(例如,如果 GUI 被阻止,您不能通过单击按钮取消或暂停复制操作).

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).

你可以在这里查看一个相当广泛的 Qt 4 类:http://docs.huihoo.com/qt/solutions/4/qtcopydialog/qtfilecopier.html 但由于其复杂性,我不确定这是否会有所帮助.

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.

相关文章