显示数据集下载进度
场景:需要下载一个数据集(有很多图片),界面有一个进度条显示下载进度
下载类DataSetLoad
DataSetLoad::UploadDir(const QString & srcPath, const QString & desPath, std::function<void(int)> pfunc)
{
//遍历要下载的文件夹:
QDirIterator dir_iterator(srcPath, QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
QMap<QString, QString> qMapList;
while (dir_iterator.hasNext())
{
qMapList[filePath] = remotePath;//把下载文件所需要的信息放到map里
}
//计算进度:总文件的数量。已经下载了的文件数量
int i = 0;
int nCount = qMapList.size();
for (auto it = qMapList.begin(); it != qMapList.end(); it++)
{
if(pfunc != nullptr)
pfunc(((float)i++ )*100/ nCount);//计算下载的百分比
downLoadOneFile();//下载一个文件
}
}
界面显示进度条
ui.ProgressBar->SetValue(0);
auto pfun = [&](int nVal) {
ui.ProgressBar->SetValue(nVal);
};
DataSetLoad::UploadDir(srcPath, desRootPath, pfun);
这样加进度条就不用改多少别人的代码了
显示错误信息
calculate.cpp文件定义一个处理函数doSomething(…),处理过程中产生一些信息 infomation=getSomeInformation(…);,把这些信息传给回调函数
//calculate.cpp文件
std::function<void(std::string, std::string)> m_pCallBack = nullptr;
bool calculate::SetLogCallBack(std::function<void(std::string, std::string)>& pCallBack)
{
m_pCallBack = pCallBack;
return true;
}
bool calculate::doSomething(...)
{
infomation=getSomeInformation(...);
m_pCallBack(infomation[0], infomation[1]);
}
qt 处理界面
void Process::Slot_Inspect()
{
auto pFunc = [&](std::string strTyp, std::string strMsg)
{
m_ui.textBrowser->append(QString((strTyp + ":" + strMsg).c_str()));
};
calculate->SetLogCallBack(pFunc);
calculate->doSomething(...);
}
这样在界面上就能打印错误信息了(这里是打印到textBrowser)