[Solved] Why my function cannot call itself?
-
Hello, I'm new to Qt environment.
Currently, I want to do a recursive in the function.
What I am doing right now is, I want to copy a folder which have another folder in it and another folder in the folder in the folder and so on and of course, the files too.
But, when I do the coding as below, the system always crash.I already tried to find the cause of this crash, but didn't find.
Please help me.This is what appears in in my Application Output,
"The program has unexpectedly finished.
G:\Qt (keje)\Project\build-Hash_Verification2-Desktop_Qt_5_3_MSVC2010_OpenGL_32bit_Microsoft_Visual_C_Compiler_10_0_x86_Local_PC-Debug\debug\Hash_Verification2.exe crashed"And this is my code snippet:
@
void MainWindow::copy(QString string, QModelIndex indexVoidDest)
{
QDir dir(string);
foreach(const QFileInfo item, dir.entryInfoList())
{
if (item.isFile())
{
QFile destinationFile,sourceFile;
QDir destination(indexVoidDest.data(QFileSystemModel::FilePathRole).toString());
QString fileName(item.fileName());destinationFile.setFileName(destination.filePath(fileName)); sourceFile.setFileName(item.absoluteFilePath()); sourceFile.copy(destinationFile.fileName()); } else if (item.isDir()) { filemodel->mkdir(indexVoidDest,item.fileName()); QString StringSe = (string + "/" + item.baseName()); // I think the error is here QModelIndex indexSeDest = filemodel->index(indexVoidDest.data(QFileSystemModel::FilePathRole).toString() + "/" + item.baseName()); copy(StringSe,indexSeDest); // I think the error is here } }
}
@ -
Hello,
You have to use the filter flag "QDir::NoDotAndDotDot" otherwise you will copy the parent and the current directories too. I think your problem comes from here.@dir.entryInfoList(QDir::NoDotAndDotDot)@
-
Hi,
I found this function to copy dirs/subdirs including its contents recursively on github.com:
@static bool copyRecursively(const QString &srcFilePath,
const QString &tgtFilePath)
{
QFileInfo srcFileInfo(srcFilePath);
if (srcFileInfo.isDir())
{
QDir targetDir(tgtFilePath);
targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
return false;
QDir sourceDir(srcFilePath);
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
foreach (const QString &fileName, fileNames)
{
const QString newSrcFilePath = srcFilePath + QLatin1Char('/') + fileName;
const QString newTgtFilePat = tgtFilePath + QLatin1Char('/') + fileName;
if (!copyRecursively(newSrcFilePath, newTgtFilePath))
return false;
}
}
else
{
if (!QFile::copy(srcFilePath, tgtFilePath))
return false;
}
return true;
} @I did not test it, but it looks right :-P
-
Thanks to both of you... :)
Now I can do my recursive... :Dkhryleption, you are right, but need to define more in it...
Binary91, thanks for giving me idea... It's very helpful... I don't really understand the code and I don't really know to call the function... :P
So, the solution, I combine both of your idea and create a new one... :D
@
foreach(const QFileInfo item, dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System));
@ -
You're welcome.
Great to hear that it works for you, but if you're interested in something from the algorithm above that you don't understand, just ask for it ;-)
-
Thanks, Binary91...
Maybe you can try to explain on the following question:
- How do we call the function in the main cpp? For example,
@
copyRecursively(sourceFile, targetFile)
@Is it true??? Just the bool function need to return the value whether it is true or false, does it???
2)Why there must be static??? Maybe you can explain a little about it...
Thanks in advance if you can answer it...
-
1: Yes, exactly. You just pass the sourceFile (folder you want to copy) and targetFile (path where the source should be copied to).
The boolean return value informs you whether copying was successful or not.2: It doesn't "have to" be static, but a static function can be called without creating an object first, hence you can use this function everywhere in your code to copy folders without creating instances of a class.
This functionallity is useful for often used functions. An example is the public static function QFile::exist(). You can check for existance of any file anywhere in your code and you don't have to create a QFile object first to call it ;-) -
You can use QDirIterator: it will not be recursive, there will be less code, and it will work faster (this class uses some important IO optimizations).
Like so:
@
QDirIterator it("directory/path", QDir::NoDotAndDotDot | QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();// mkpath to the new file, using it.fileInfo() // copy, using it.fileInfo().
}
}
@ -
Binary91,
thanks for the clarification...sierdzio,
I will try to use it... :)