QT - QFile copy operation extremely slow

半城伤御伤魂 提交于 2019-12-06 14:14:27

The reason that your change broke QFile is that a 4M buffer won't fit on the stack (the default stack size is typically something like 1M). A quick fix would be:

std::vector<char> vec(4*1024*1024);
char *block = &vec.front();

The vector will allocate the big buffer on the heap (and take care of deallocating when you are done), and you just point block at the front of the vector.

I think your analysis of why copy is slow is spot on.

Well, changing the buffer size did no good, since that apparently is just a fallback in case the derived function engine()->copy() fails. I don't know exactly how that function works, nor did I want to waste time modifying core QT engine classes to make this work.

In the end, since my project was only supposed to run on Windows, I ended up using the native Win32 copy function. So I replaced my call to:

QFile::copy(src, dest);

with:

CopyFileExW((LPCWSTR)src.utf16(), (LPCWSTR)dest.utf16(), 0, this, 0, 0);

Note that you must #include "windows.h" for this invocation to work.

This seems to be no more an issue with newer version of Qt (I am using 5.9.2). Please have a look at QFileSystemEngine::copyFile() in https://code.woboq.org/qt5/qtbase/src/corelib/io/qfilesystemengine_win.cpp.html The code uses native function CopyFile2. Also my testing confirmed that QFile::copy() is on par with the native implementation on Windows. Seems Qt has made some progress in this area.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!