复制文件的进度条
Progress bar for copying files
我正在尝试为Qt中的文件副本创建进度条。这是我所能找到的最接近的,但我认为这不起作用,因为根据Qt类文档:
与其他QIODevice实现(如QTcpSocket)不同,QFile不发出aboutToClose()、bytesWritten()或readyRead()信号。此实现细节意味着QFile不适合读取和写入某些类型的文件,例如上的设备文件Unix平台。
我怎么能做这样的事?我不知道如何实现我自己的信号。
这是我的代码:
void Replicator::anotbaandbFile(QDir source, QDir target)
{
source.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
target.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
qDebug() << "Scanning: " << source.path();
QStringList sourceFileList = source.entryList();
QStringList targetFileList = target.entryList();
for (int aCount = 0; aCount < sourceFileList.count(); aCount++)
{
bool found = false;
for (int bCount = 0; bCount < targetFileList.count(); bCount++)
if (sourceFileList.at(aCount) == targetFileList.at(bCount))
found = true;
if (found == false)
{
sourceFile = new QFile(source.absolutePath()+"/"+sourceFileList.at(aCount));
targetFile = new QFile(target.absolutePath()+"/"+sourceFileList.at(aCount));
progressBar->setMinimum(0);
progressBar->setMaximum(sourceFile->size());
written = 0;
connect(sourceFile,SIGNAL(bytesWritten(qint64)),SLOT(onWrite(qint64)));
sourceFile->copy(targetFile->fileName());
//QFile::copy(source.absolutePath()+"/"+sourceFileList.at(aCount), target.absolutePath()+"/"+sourceFileList.at(aCount));
qDebug() << source.absolutePath()+"/"+sourceFileList.at(aCount) << " " << target.absolutePath()+"/"+sourceFileList.at(aCount);
}
}
}
和
void Replicator::onWrite(qint64 w)
{
written += w;
progressBar->setValue( written );
}
从上述修改的新代码
if (found == false)
{
sourceFile = new QFile(source.absolutePath()+"/"+sourceFileList.at(aCount));
targetFile = new QFile(target.absolutePath()+"/"+sourceFileList.at(aCount));
progressBar->setMinimum(0);
progressBar->setMaximum(sourceFile->size());
QByteArray buffer;
for (int count = 0; !(buffer = sourceFile->read(1000000)).isEmpty(); count+=1000000)
{
targetFile->write(buffer);
progressBar->setValue(count);
}
//targetFile->write(buffer);
//QFile::copy(source.absolutePath()+"/"+sourceFileList.at(aCount), target.absolutePath()+"/"+sourceFileList.at(aCount));
qDebug() << "copying " << sourceFile->fileName() << " to " << targetFile->fileName();
}
您可以简单地按固定大小的部分复制大文件,而不是计算已经复制的部分,并通过将其除以全部部分来计算工作量的百分比。
int iWorkPercentage = (int)(((float)iPortionsProcessed / (float)iOveralPortions) * 100);
相关文章:
- 如何使用位字段将数据从二进制文件复制到结构中?
- 将文件复制到自定义位置,存在字符串转换问题
- 无法将整个文本文件复制到字符数组
- 使用 Libssh 将文件从服务器复制到客户端:分配文件复制目标路径时出现问题
- 将文本从一个文件复制到另一个c++流时出错
- 如何检查文件复制和写入是否成功
- 将文件复制到 pro/pri 文件中的新目录
- C++可以从文本文件复制并粘贴到打开的Word程序中
- 如何使用 c++ 文件系统库将文件复制到另一个目录
- 将.bin输入文件复制到 linux 中
- CMake:将头文件复制到输出目录
- 如何将文件复制到另一个文件中,但是用用户输入的单词替换单词
- 仅将所需的数据从一个大文件复制到另一个大文件
- catkin_make无法创建可执行文件,并自动将标题文件复制到DEVEL
- QT-将文件复制到AppData
- 将文件复制到另一个目录不起作用
- 使用boost递归地将给定目录中每个最低级别文件夹的文件复制到另一个目录-C++
- 在 c++ 中将整个.txt文件复制到 char 数组中
- 将二进制文件复制到C++中的远程位置
- 我试图运行将文本从一个文件复制到另一个文件的程序