Qt 拷贝文件并显示进度百分比

1. windows系统,利用cmd命令 copy文件,同时读取进度,显示在界面中

	            QProcess* p = new QProcess;
                QString strSrc = sourceDir.filePath(fileInfo.fileName());
                QString strDst = destinationDir.filePath(fileInfo.fileName()).left(destinationDir.filePath(fileInfo.fileName()).lastIndexOf("/"));

                p->setReadChannel(QProcess::StandardOutput );
                QObject::connect(p, &QProcess::readyReadStandardOutput, this, [&] ()
                {
                    QByteArray byteArr = p->readAll();
                    int nSize = byteArr.size();
                    QString strContent = byteArr;
                    strContent = strContent.fromLocal8Bit(byteArr);
                    QStringList strList = strContent.split("\r", QString::SkipEmptyParts);
                    strList.removeDuplicates();
                    if (!strList.isEmpty())
                        strContent = strList.at(0).left(strList.at(0).indexOf("%") + 1);
                    else
                        strContent = "复制完成";

                    QString strShowText = QString("正在复制文件 ") + fileInfo.fileName();
                    strShowText += "\t\t\t" + strContent;
                    sendCopyFileProgress(strShowText);
                    qDebug() <<"output : " << strContent;
                });

                QString strCommand = QString("/C copy /y /z %1 %2").arg(strSrc.replace("/", "\\")).arg(strDst.replace("/", "\\"));
                p->start("cmd", QStringList() << strCommand);
                p->waitForFinished(-1);
                qDebug()<< "command is : " << strCommand;
                delete p;
                p = NULL;


版权声明:本文为hello_zhengyue原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。